home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Modules / cPickle.c < prev    next >
C/C++ Source or Header  |  1998-05-27  |  99KB  |  4,349 lines

  1. /*
  2.      cPickle.c,v 1.48 1997/12/07 14:37:39 jim Exp
  3.  
  4.      Copyright 
  5.  
  6.        Copyright 1996 Digital Creations, L.C., 910 Princess Anne
  7.        Street, Suite 300, Fredericksburg, Virginia 22401 U.S.A. All
  8.        rights reserved.  Copyright in this software is owned by DCLC,
  9.        unless otherwise indicated. Permission to use, copy and
  10.        distribute this software is hereby granted, provided that the
  11.        above copyright notice appear in all copies and that both that
  12.        copyright notice and this permission notice appear. Note that
  13.        any product, process or technology described in this software
  14.        may be the subject of other Intellectual Property rights
  15.        reserved by Digital Creations, L.C. and are not licensed
  16.        hereunder.
  17.  
  18.      Trademarks 
  19.  
  20.        Digital Creations & DCLC, are trademarks of Digital Creations, L.C..
  21.        All other trademarks are owned by their respective companies. 
  22.  
  23.      No Warranty 
  24.  
  25.        The software is provided "as is" without warranty of any kind,
  26.        either express or implied, including, but not limited to, the
  27.        implied warranties of merchantability, fitness for a particular
  28.        purpose, or non-infringement. This software could include
  29.        technical inaccuracies or typographical errors. Changes are
  30.        periodically made to the software; these changes will be
  31.        incorporated in new editions of the software. DCLC may make
  32.        improvements and/or changes in this software at any time
  33.        without notice.
  34.  
  35.      Limitation Of Liability 
  36.  
  37.        In no event will DCLC be liable for direct, indirect, special,
  38.        incidental, economic, cover, or consequential damages arising
  39.        out of the use of or inability to use this software even if
  40.        advised of the possibility of such damages. Some states do not
  41.        allow the exclusion or limitation of implied warranties or
  42.        limitation of liability for incidental or consequential
  43.        damages, so the above limitation or exclusion may not apply to
  44.        you.
  45.  
  46.     If you have questions regarding this software,
  47.     contact:
  48.    
  49.       Jim Fulton, jim@digicool.com
  50.       Digital Creations L.C.  
  51.    
  52.       (540) 371-6909
  53. */
  54.  
  55. static char cPickle_module_documentation[] = 
  56. "C implementation and optimization of the Python pickle module\n"
  57. "\n"
  58. "cPickle.c,v 1.48 1997/12/07 14:37:39 jim Exp\n"
  59. ;
  60.  
  61. #include "Python.h"
  62. #include "cStringIO.h"
  63. #include "mymath.h"
  64.  
  65. #ifndef Py_eval_input
  66. #include <graminit.h>
  67. #define Py_eval_input eval_input
  68. #endif /* Py_eval_input */
  69.  
  70. #include <errno.h>
  71.  
  72. #ifdef _AMIGA
  73. #ifdef GLOBAL
  74. #undef GLOBAL
  75. #endif
  76. #endif
  77.  
  78. #define UNLESS(E) if (!(E))
  79.  
  80. #define DEL_LIST_SLICE(list, from, to) (PyList_SetSlice(list, from, to, NULL))
  81.  
  82. #define WRITE_BUF_SIZE 256
  83.  
  84.  
  85. #define MARK        '('
  86. #define STOP        '.'
  87. #define POP         '0'
  88. #define POP_MARK    '1'
  89. #define DUP         '2'
  90. #define FLOAT       'F'
  91. #define BINFLOAT    'G'
  92. #define INT         'I'
  93. #define BININT      'J'
  94. #define BININT1     'K'
  95. #define LONG        'L'
  96. #define BININT2     'M'
  97. #define NONE        'N'
  98. #define PERSID      'P'
  99. #define BINPERSID   'Q'
  100. #define REDUCE      'R'
  101. #define STRING      'S'
  102. #define BINSTRING   'T'
  103. #define SHORT_BINSTRING 'U'
  104. #define APPEND      'a'
  105. #define BUILD       'b'
  106. #define GLOBAL      'c'
  107. #define DICT        'd'
  108. #define EMPTY_DICT  '}'
  109. #define APPENDS     'e'
  110. #define GET         'g'
  111. #define BINGET      'h'
  112. #define INST        'i'
  113. #define LONG_BINGET 'j'
  114. #define LIST        'l'
  115. #define EMPTY_LIST  ']'
  116. #define OBJ         'o'
  117. #define PUT         'p'
  118. #define BINPUT      'q'
  119. #define LONG_BINPUT 'r'
  120. #define SETITEM     's'
  121. #define TUPLE       't'
  122. #define EMPTY_TUPLE ')'
  123. #define SETITEMS    'u'
  124.  
  125. static char MARKv = MARK;
  126.  
  127. /* atol function from string module */
  128. static PyObject *atol_func;
  129.  
  130. static PyObject *PicklingError;
  131. static PyObject *UnpicklingError;
  132.  
  133. static PyObject *dispatch_table;
  134. static PyObject *safe_constructors;
  135. static PyObject *class_map;
  136. static PyObject *empty_tuple;
  137.  
  138. static PyObject *__class___str, *__getinitargs___str, *__dict___str,
  139.   *__getstate___str, *__setstate___str, *__name___str, *__reduce___str,
  140.   *write_str, *__safe_for_unpickling___str, *append_str,
  141.   *read_str, *readline_str, *__main___str, *__basicnew___str,
  142.   *copy_reg_str, *dispatch_table_str, *safe_constructors_str;
  143.  
  144. typedef struct _Picklerobject_struct {
  145.      PyObject_HEAD
  146.      FILE *fp;
  147.      PyObject *write;
  148.      PyObject *file;
  149.      PyObject *memo;
  150.      PyObject *arg;
  151.      PyObject *pers_func;
  152.      PyObject *inst_pers_func;
  153.      int bin;
  154.      int (*write_func)Py_PROTO((struct _Picklerobject_struct *self, char *s, int n));
  155.      char *write_buf;
  156.      int buf_size;
  157.      PyObject *dispatch_table;
  158.      PyObject *class_map;
  159. } Picklerobject;
  160.  
  161. static int save Py_PROTO((Picklerobject *self, PyObject *args, int  pers_save));
  162. static int put2 Py_PROTO((Picklerobject *self, PyObject *ob));
  163.  
  164. staticforward PyTypeObject Picklertype;
  165.  
  166. typedef struct _Unpicklerobject_struct {
  167.      PyObject_HEAD
  168.      FILE *fp;
  169.      PyObject *file;
  170.      PyObject *readline;
  171.      PyObject *read;
  172.      PyObject *memo;
  173.      PyObject *arg;
  174.      PyObject *stack;
  175.      PyObject *mark;
  176.      PyObject *pers_func;
  177.      PyObject *last_string;
  178.      int *marks;
  179.      int num_marks;
  180.      int marks_size;
  181.      int (*read_func)Py_PROTO((struct _Unpicklerobject_struct *self, char **s, int n));
  182.      int (*readline_func)Py_PROTO((struct _Unpicklerobject_struct *self, char **s));
  183.      int buf_size;
  184.      char *buf;
  185.      PyObject *safe_constructors;
  186.      PyObject *class_map;
  187. } Unpicklerobject;
  188.  
  189. staticforward PyTypeObject Unpicklertype;
  190.  
  191. int 
  192. cPickle_PyMapping_HasKey(PyObject *o, PyObject *key) {
  193.     PyObject *v;
  194.  
  195.     if((v = PyObject_GetItem(o,key))) {
  196.         Py_DECREF(v);
  197.         return 1;
  198.     }
  199.  
  200.     PyErr_Clear();
  201.     return 0;
  202. }
  203.  
  204. static
  205. PyObject *
  206. #ifdef HAVE_STDARG_PROTOTYPES
  207. /* VARARGS 2 */
  208. cPickle_ErrFormat(PyObject *ErrType, char *stringformat, char *format, ...) {
  209. #else
  210. /* VARARGS */
  211. cPickle_ErrFormat(va_alist) va_dcl {
  212. #endif
  213.   va_list va;
  214.   PyObject *args=0, *retval=0;
  215. #ifdef HAVE_STDARG_PROTOTYPES
  216.   va_start(va, format);
  217. #else
  218.   PyObject *ErrType;
  219.   char *stringformat, *format;
  220.   va_start(va);
  221.   ErrType = va_arg(va, PyObject *);
  222.   stringformat   = va_arg(va, char *);
  223.   format   = va_arg(va, char *);
  224. #endif
  225.   
  226.   if(format) args = Py_VaBuildValue(format, va);
  227.   va_end(va);
  228.   if(format && ! args) return NULL;
  229.   if(stringformat && !(retval=PyString_FromString(stringformat))) return NULL;
  230.  
  231.   if(retval) {
  232.       if(args) {
  233.       PyObject *v;
  234.       v=PyString_Format(retval, args);
  235.       Py_DECREF(retval);
  236.       Py_DECREF(args);
  237.       if(! v) return NULL;
  238.       retval=v;
  239.     }
  240.     }
  241.   else
  242.     if(args) retval=args;
  243.     else {
  244.     PyErr_SetObject(ErrType,Py_None);
  245.     return NULL;
  246.       }
  247.   PyErr_SetObject(ErrType,retval);
  248.   Py_DECREF(retval);
  249.   return NULL;
  250. }
  251.  
  252. static int 
  253. write_file(Picklerobject *self, char *s, int  n) {
  254.     if (s == NULL) {
  255.         return 0;
  256.     }
  257.  
  258.     if ((int)fwrite(s, sizeof(char), n, self->fp) != n) {
  259.         PyErr_SetFromErrno(PyExc_IOError);
  260.         return -1;
  261.     }
  262.  
  263.     return n;
  264. }
  265.  
  266.  
  267. static int 
  268. write_cStringIO(Picklerobject *self, char *s, int  n) {
  269.     if (s == NULL) {
  270.         return 0;
  271.     }
  272.  
  273.     if (PycStringIO->cwrite((PyObject *)self->file, s, n) != n) {
  274.         return -1;
  275.     }
  276.  
  277.     return n;
  278. }
  279.  
  280.  
  281. static int 
  282. write_none(Picklerobject *self, char *s, int  n) {
  283.     if (s == NULL) return 0;
  284.     return n;
  285. }
  286.  
  287.  
  288. static int 
  289. write_other(Picklerobject *self, char *s, int  n) {
  290.     PyObject *py_str = 0, *junk = 0;
  291.     int res = -1;
  292.  
  293.     if (s == NULL) {
  294.         UNLESS(self->buf_size) return 0;
  295.         UNLESS(py_str = 
  296.             PyString_FromStringAndSize(self->write_buf, self->buf_size))
  297.             goto finally;
  298.     }
  299.     else {
  300.         if (self->buf_size && (n + self->buf_size) > WRITE_BUF_SIZE) {
  301.             if (write_other(self, NULL, 0) < 0)
  302.                 goto finally;
  303.         }
  304.  
  305.         if (n > WRITE_BUF_SIZE) {    
  306.             UNLESS(py_str = 
  307.                 PyString_FromStringAndSize(s, n))
  308.                 goto finally;
  309.         }
  310.         else {
  311.             memcpy(self->write_buf + self->buf_size, s, n);
  312.             self->buf_size += n;
  313.             res = n;
  314.             goto finally;
  315.         }
  316.     }
  317.  
  318.     UNLESS(self->arg)
  319.         UNLESS(self->arg = PyTuple_New(1))
  320.             goto finally;
  321.  
  322.     Py_INCREF(py_str);
  323.     if (PyTuple_SetItem(self->arg, 0, py_str) < 0)
  324.         goto finally;
  325.  
  326.     UNLESS(junk = PyObject_CallObject(self->write, self->arg))
  327.         goto finally;
  328.     Py_DECREF(junk);
  329.  
  330.     self->buf_size = 0;
  331.  
  332.     res = n;
  333.  
  334. finally:
  335.     Py_XDECREF(py_str);
  336.  
  337.     return res;
  338. }
  339.  
  340.  
  341. static int 
  342. read_file(Unpicklerobject *self, char **s, int  n) {
  343.  
  344.     if (self->buf_size == 0) {
  345.         int size;
  346.  
  347.         size = ((n < 32) ? 32 : n); 
  348.         UNLESS(self->buf = (char *)malloc(size * sizeof(char))) {
  349.             PyErr_NoMemory();
  350.             return -1;
  351.         }
  352.  
  353.         self->buf_size = size;
  354.     }
  355.     else if (n > self->buf_size) {
  356.         UNLESS(self->buf = (char *)realloc(self->buf, n * sizeof(char))) {
  357.             PyErr_NoMemory();
  358.             return -1;
  359.         }
  360.  
  361.         self->buf_size = n;
  362.     }
  363.             
  364.     if ((int)fread(self->buf, sizeof(char), n, self->fp) != n) {  
  365.         if (feof(self->fp)) {
  366.             PyErr_SetNone(PyExc_EOFError);
  367.             return -1;
  368.         }
  369.  
  370.         PyErr_SetFromErrno(PyExc_IOError);
  371.         return -1;
  372.     }
  373.  
  374.     *s = self->buf;
  375.  
  376.     return n;
  377. }
  378.  
  379.  
  380. static int 
  381. readline_file(Unpicklerobject *self, char **s) {
  382.     int i;
  383.  
  384.     if (self->buf_size == 0) {
  385.         UNLESS(self->buf = (char *)malloc(40 * sizeof(char))) {
  386.             PyErr_NoMemory();
  387.             return -1;
  388.         }
  389.    
  390.         self->buf_size = 40;
  391.     }
  392.  
  393.     i = 0;
  394.     while (1) {
  395.         for (; i < (self->buf_size - 1); i++) {
  396.             if (feof(self->fp) || (self->buf[i] = getc(self->fp)) == '\n') {
  397.                 self->buf[i + 1] = '\0';
  398.                 *s = self->buf;
  399.                 return i + 1;
  400.             }
  401.         }
  402.  
  403.         UNLESS(self->buf = (char *)realloc(self->buf, 
  404.             (self->buf_size * 2) * sizeof(char))) {
  405.             PyErr_NoMemory();
  406.             return -1;
  407.         }
  408.  
  409.         self->buf_size *= 2;
  410.     }
  411.  
  412. }    
  413.  
  414.  
  415. static int 
  416. read_cStringIO(Unpicklerobject *self, char **s, int  n) {
  417.     char *ptr;
  418.  
  419.     if (PycStringIO->cread((PyObject *)self->file, &ptr, n) != n) {
  420.         PyErr_SetNone(PyExc_EOFError);
  421.         return -1;
  422.     }
  423.  
  424.     *s = ptr;
  425.  
  426.     return n;
  427. }
  428.  
  429.  
  430. static int 
  431. readline_cStringIO(Unpicklerobject *self, char **s) {
  432.     int n;
  433.     char *ptr;
  434.  
  435.     if ((n = PycStringIO->creadline((PyObject *)self->file, &ptr)) < 0) {
  436.         return -1;
  437.     }
  438.  
  439.     *s = ptr;
  440.  
  441.     return n;
  442. }
  443.  
  444.  
  445. static int 
  446. read_other(Unpicklerobject *self, char **s, int  n) {
  447.     PyObject *bytes, *str;
  448.     int res = -1;
  449.  
  450.     UNLESS(bytes = PyInt_FromLong(n)) {
  451.         if (!PyErr_Occurred())
  452.             PyErr_SetNone(PyExc_EOFError);
  453.  
  454.         goto finally;
  455.     }
  456.  
  457.     UNLESS(self->arg)
  458.         UNLESS(self->arg = PyTuple_New(1))
  459.             goto finally;
  460.  
  461.     Py_INCREF(bytes);
  462.     if (PyTuple_SetItem(self->arg, 0, bytes) < 0)
  463.         goto finally;
  464.  
  465.     UNLESS(str = PyObject_CallObject(self->read, self->arg))
  466.         goto finally;
  467.  
  468.     Py_XDECREF(self->last_string);
  469.     self->last_string = str;
  470.  
  471.     *s = PyString_AsString(str);
  472.  
  473.     res = n;
  474.  
  475. finally:
  476.      Py_XDECREF(bytes);
  477.  
  478.      return res;
  479. }
  480.  
  481.  
  482. static int 
  483. readline_other(Unpicklerobject *self, char **s) {
  484.     PyObject *str;
  485.     int str_size;
  486.  
  487.     UNLESS(str = PyObject_CallObject(self->readline, empty_tuple)) {
  488.         return -1;
  489.     }
  490.  
  491.     str_size = PyString_Size(str);
  492.  
  493.     Py_XDECREF(self->last_string);
  494.     self->last_string = str;
  495.  
  496.     *s = PyString_AsString(str);
  497.  
  498.     return str_size;
  499. }
  500.  
  501.  
  502. static char *
  503. pystrndup(char *s, int l) {
  504.   char *r;
  505.   UNLESS(r=malloc((l+1)*sizeof(char))) return (char*)PyErr_NoMemory();
  506.   memcpy(r,s,l);
  507.   r[l]=0;
  508.   return r;
  509. }
  510.  
  511.  
  512. static int
  513. get(Picklerobject *self, PyObject *id) {
  514.     PyObject *value = 0;
  515.     long c_value;
  516.     char s[30];
  517.     int len;
  518.  
  519.     UNLESS(value = PyDict_GetItem(self->memo, id))
  520.         return -1;
  521.  
  522.     UNLESS(value = PyTuple_GetItem(value, 0))
  523.         return -1;
  524.         
  525.     c_value = PyInt_AsLong(value);
  526.  
  527.     if (!self->bin) {
  528.         s[0] = GET;
  529.         sprintf(s + 1, "%ld\n", c_value);
  530.         len = strlen(s);
  531.     }
  532.     else {
  533.         if (c_value < 256) {
  534.             s[0] = BINGET;
  535.             s[1] = (int)(c_value & 0xff);
  536.             len = 2;
  537.         }
  538.         else {
  539.             s[0] = LONG_BINGET;
  540.             s[1] = (int)(c_value & 0xff);
  541.             s[2] = (int)((c_value >> 8)  & 0xff);
  542.             s[3] = (int)((c_value >> 16) & 0xff);
  543.             s[4] = (int)((c_value >> 24) & 0xff);
  544.             len = 5;
  545.         }
  546.     }
  547.  
  548.     if ((*self->write_func)(self, s, len) < 0)
  549.         return -1;
  550.  
  551.     return 0;
  552. }
  553.     
  554.  
  555. static int
  556. put(Picklerobject *self, PyObject *ob) {
  557.     if (ob->ob_refcnt < 2)
  558.         return 0;
  559.  
  560.     return put2(self, ob);
  561. }
  562.  
  563.   
  564. static int
  565. put2(Picklerobject *self, PyObject *ob) {
  566.     char c_str[30];
  567.     int p, len, res = -1;
  568.     PyObject *py_ob_id = 0, *memo_len = 0, *t = 0;
  569.     if ((p = PyDict_Size(self->memo)) < 0)
  570.         goto finally;
  571.  
  572.     if (!self->bin) {
  573.         c_str[0] = PUT;
  574.         sprintf(c_str + 1, "%d\n", p);
  575.         len = strlen(c_str);
  576.     }
  577.     else {
  578.         if (p >= 256) {
  579.             c_str[0] = LONG_BINPUT;
  580.             c_str[1] = (int)(p & 0xff);
  581.             c_str[2] = (int)((p >> 8)  & 0xff);
  582.             c_str[3] = (int)((p >> 16) & 0xff);
  583.             c_str[4] = (int)((p >> 24) & 0xff);
  584.             len = 5;
  585.         }
  586.         else {
  587.             c_str[0] = BINPUT;
  588.             c_str[1] = p;
  589.             len = 2; 
  590.         }
  591.     }
  592.  
  593.     if ((*self->write_func)(self, c_str, len) < 0)
  594.         goto finally;
  595.  
  596.     UNLESS(py_ob_id = PyInt_FromLong((long)ob))
  597.         goto finally;
  598.  
  599.     UNLESS(memo_len = PyInt_FromLong(p))
  600.         goto finally;
  601.  
  602.     UNLESS(t = PyTuple_New(2))
  603.         goto finally;
  604.  
  605.     PyTuple_SET_ITEM(t, 0, memo_len);
  606.     Py_INCREF(memo_len);
  607.     PyTuple_SET_ITEM(t, 1, ob);
  608.     Py_INCREF(ob);
  609.  
  610.     if (PyDict_SetItem(self->memo, py_ob_id, t) < 0)
  611.         goto finally;
  612.  
  613.     res = 0;
  614.  
  615. finally:
  616.     Py_XDECREF(py_ob_id);
  617.     Py_XDECREF(memo_len);
  618.     Py_XDECREF(t);
  619.  
  620.     return res;
  621. }
  622.  
  623. #define PyImport_Import cPickle_Import
  624.  
  625. static PyObject *
  626. PyImport_Import(PyObject *module_name) {
  627.   static PyObject *silly_list=0, *__builtins___str=0, *__import___str;
  628.   static PyObject *standard_builtins=0;
  629.   PyObject *globals=0, *__import__=0, *__builtins__=0, *r=0;
  630.  
  631.   UNLESS(silly_list) {
  632.       UNLESS(__import___str=PyString_FromString("__import__")) return NULL;
  633.       UNLESS(__builtins___str=PyString_FromString("__builtins__")) return NULL;
  634.       UNLESS(silly_list=Py_BuildValue("[s]","__doc__")) return NULL;
  635.     }
  636.  
  637.   if((globals=PyEval_GetGlobals())) {
  638.       Py_INCREF(globals);
  639.       UNLESS(__builtins__=PyObject_GetItem(globals,__builtins___str)) goto err;
  640.     }
  641.   else {
  642.       PyErr_Clear();
  643.  
  644.       UNLESS(standard_builtins ||
  645.          (standard_builtins=PyImport_ImportModule("__builtin__")))
  646.     return NULL;
  647.       
  648.       __builtins__=standard_builtins;
  649.       Py_INCREF(__builtins__);
  650.       UNLESS(globals = Py_BuildValue("{sO}", "__builtins__", __builtins__))
  651.     goto err;
  652.     }
  653.  
  654.   if(PyDict_Check(__builtins__)) {
  655.     UNLESS(__import__=PyObject_GetItem(__builtins__,__import___str)) goto err;
  656.   }
  657.   else {
  658.     UNLESS(__import__=PyObject_GetAttr(__builtins__,__import___str)) goto err;
  659.   }
  660.  
  661.   UNLESS(r=PyObject_CallFunction(__import__,"OOOO",
  662.                  module_name, globals, globals, silly_list))
  663.     goto err;
  664.  
  665.   Py_DECREF(globals);
  666.   Py_DECREF(__builtins__);
  667.   Py_DECREF(__import__);
  668.   
  669.   return r;
  670. err:
  671.   Py_XDECREF(globals);
  672.   Py_XDECREF(__builtins__);
  673.   Py_XDECREF(__import__);
  674.   return NULL;
  675. }
  676.  
  677. static PyObject *
  678. whichmodule(PyObject *class_map, PyObject *global, PyObject *global_name) {
  679.     int i, j;
  680.     PyObject *module = 0, *modules_dict = 0,
  681.         *global_name_attr = 0, *name = 0;
  682.  
  683.     module = PyObject_GetAttrString(global, "__module__");
  684.     if (module) return module;
  685.     PyErr_Clear();
  686.  
  687.     if ((module = PyDict_GetItem(class_map, global))) {
  688.         Py_INCREF(module);
  689.         return module;
  690.     }
  691.     else {
  692.         PyErr_Clear();
  693.     }
  694.  
  695.     UNLESS(modules_dict = PySys_GetObject("modules"))
  696.         return NULL;
  697.  
  698.     i = 0;
  699.     while ((j = PyDict_Next(modules_dict, &i, &name, &module))) {
  700.  
  701.         if(PyObject_Compare(name, __main___str)==0) continue;
  702.       
  703.         UNLESS(global_name_attr = PyObject_GetAttr(module, global_name)) {
  704.             PyErr_Clear();
  705.             continue;
  706.         }
  707.  
  708.         if (global_name_attr != global) {
  709.             Py_DECREF(global_name_attr);
  710.             continue;
  711.         }
  712.  
  713.         Py_DECREF(global_name_attr);
  714.  
  715.         break;
  716.     }
  717.  
  718.     /* The following implements the rule in pickle.py added in 1.5
  719.        that used __main__ if no module is found.  I don't actually
  720.        like this rule. jlf
  721.      */
  722.     if(!j) {
  723.         j=1;
  724.         name=__main___str;
  725.     }
  726.     
  727.     /*
  728.     if (!j) {
  729.         cPickle_ErrFormat(PicklingError, "Could not find module for %s.", 
  730.             "O", global_name);
  731.         return NULL;
  732.     }
  733.     */
  734.  
  735.     PyDict_SetItem(class_map, global, name);
  736.  
  737.     Py_INCREF(name);
  738.     return name;
  739. }
  740.  
  741.  
  742. static int
  743. save_none(Picklerobject *self, PyObject *args) {
  744.     static char none = NONE;
  745.     if ((*self->write_func)(self, &none, 1) < 0)  
  746.         return -1;
  747.  
  748.     return 0;
  749. }
  750.  
  751.       
  752. static int
  753. save_int(Picklerobject *self, PyObject *args) {
  754.     char c_str[32];
  755.     long l = PyInt_AS_LONG((PyIntObject *)args);
  756.     int len = 0;
  757.  
  758.     if (!self->bin
  759. #if SIZEOF_LONG > 4
  760.     || (l >> 32)
  761. #endif
  762.         ) {
  763.             /* Save extra-long ints in non-binary mode, so that
  764.            we can use python long parsing code to restore,
  765.            if necessary. */
  766.         c_str[0] = INT;
  767.         sprintf(c_str + 1, "%ld\n", l);
  768.         if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
  769.             return -1;
  770.     }
  771.     else {
  772.         c_str[1] = (int)( l        & 0xff);
  773.         c_str[2] = (int)((l >> 8)  & 0xff);
  774.         c_str[3] = (int)((l >> 16) & 0xff);
  775.         c_str[4] = (int)((l >> 24) & 0xff);
  776.  
  777.         if ((c_str[4] == 0) && (c_str[3] == 0)) {
  778.             if (c_str[2] == 0) {
  779.                 c_str[0] = BININT1;
  780.                 len = 2;
  781.             }
  782.             else {
  783.                 c_str[0] = BININT2;
  784.                 len = 3;
  785.             }
  786.         }
  787.         else {
  788.             c_str[0] = BININT;
  789.             len = 5;
  790.         }
  791.  
  792.         if ((*self->write_func)(self, c_str, len) < 0)
  793.             return -1;
  794.     }
  795.  
  796.     return 0;
  797. }
  798.  
  799.  
  800. static int
  801. save_long(Picklerobject *self, PyObject *args) {
  802.     int size, res = -1;
  803.     PyObject *repr = 0;
  804.  
  805.     static char l = LONG;
  806.  
  807.     UNLESS(repr = PyObject_Repr(args))
  808.         goto finally;
  809.  
  810.     if ((size = PyString_Size(repr)) < 0)
  811.         goto finally;
  812.  
  813.     if ((*self->write_func)(self, &l, 1) < 0)
  814.         goto finally;
  815.  
  816.     if ((*self->write_func)(self, 
  817.         PyString_AS_STRING((PyStringObject *)repr), size) < 0)
  818.         goto finally;
  819.  
  820.     if ((*self->write_func)(self, "\n", 1) < 0)
  821.         goto finally;
  822.  
  823.     res = 0;
  824.  
  825. finally:
  826.     Py_XDECREF(repr);
  827.  
  828.     return res;
  829. }
  830.  
  831.  
  832. static int
  833. save_float(Picklerobject *self, PyObject *args) {
  834.     double x = PyFloat_AS_DOUBLE((PyFloatObject *)args);
  835.  
  836. #ifdef FORMAT_1_3
  837.     if (self->bin) {
  838.         int s, e;
  839.         double f;
  840.         long fhi, flo;
  841.         char str[9], *p = str;
  842.  
  843.         *p = BINFLOAT;
  844.         p++;
  845.  
  846.         if (x < 0) {
  847.             s = 1;
  848.             x = -x;
  849.         }
  850.         else
  851.             s = 0;
  852.  
  853.         f = frexp(x, &e);
  854.  
  855.         /* Normalize f to be in the range [1.0, 2.0) */
  856.         if (0.5 <= f && f < 1.0) {
  857.             f *= 2.0;
  858.             e--;
  859.         }
  860.         else if (f == 0.0) {
  861.             e = 0;
  862.         }
  863.         else {
  864.             PyErr_SetString(PyExc_SystemError,
  865.                             "frexp() result out of range");
  866.             return -1;
  867.         }
  868.  
  869.         if (e >= 1024) {
  870.             /* XXX 1024 itself is reserved for Inf/NaN */
  871.             PyErr_SetString(PyExc_OverflowError,
  872.                             "float too large to pack with d format");
  873.             return -1;
  874.         }
  875.         else if (e < -1022) {
  876.             /* Gradual underflow */
  877.             f = ldexp(f, 1022 + e);
  878.             e = 0;
  879.         }
  880.         else {
  881.             e += 1023;
  882.             f -= 1.0; /* Get rid of leading 1 */
  883.         }
  884.  
  885.         /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
  886.         f *= 268435456.0; /* 2**28 */
  887.         fhi = (long) floor(f); /* Truncate */
  888.         f -= (double)fhi;
  889.         f *= 16777216.0; /* 2**24 */
  890.         flo = (long) floor(f + 0.5); /* Round */
  891.  
  892.         /* First byte */
  893.         *p = (s<<7) | (e>>4);
  894.         p++;
  895.  
  896.         /* Second byte */
  897.         *p = ((e&0xF)<<4) | (fhi>>24);
  898.         p++;
  899.  
  900.         /* Third byte */
  901.         *p = (fhi>>16) & 0xFF;
  902.         p++;
  903.  
  904.         /* Fourth byte */
  905.         *p = (fhi>>8) & 0xFF;
  906.         p++;
  907.  
  908.         /* Fifth byte */
  909.         *p = fhi & 0xFF;
  910.         p++;
  911.  
  912.         /* Sixth byte */
  913.         *p = (flo>>16) & 0xFF;
  914.         p++;
  915.  
  916.         /* Seventh byte */
  917.         *p = (flo>>8) & 0xFF;
  918.         p++;
  919.  
  920.         /* Eighth byte */
  921.         *p = flo & 0xFF;
  922.  
  923.         if ((*self->write_func)(self, str, 9) < 0)
  924.             return -1;
  925.     }
  926.     else
  927. #endif
  928.     {
  929.         char c_str[250];
  930.         c_str[0] = FLOAT;
  931.         sprintf(c_str + 1, "%.17g\n", x);
  932.  
  933.         if ((*self->write_func)(self, c_str, strlen(c_str)) < 0)
  934.             return -1;
  935.     }
  936.  
  937.     return 0;
  938. }
  939.  
  940.  
  941. static int
  942. save_string(Picklerobject *self, PyObject *args, int doput) {
  943.     int size, len;
  944.  
  945.     size = PyString_Size(args);
  946.  
  947.     if (!self->bin) {
  948.         PyObject *repr;
  949.         char *repr_str;
  950.  
  951.         static char string = STRING;
  952.  
  953.         UNLESS(repr = PyObject_Repr(args))
  954.             return -1;
  955.  
  956.         repr_str = PyString_AS_STRING((PyStringObject *)repr);
  957.         len = PyString_Size(repr);
  958.  
  959.         if ((*self->write_func)(self, &string, 1) < 0)
  960.             return -1;
  961.  
  962.         if ((*self->write_func)(self, repr_str, len) < 0)
  963.             return -1;
  964.  
  965.         if ((*self->write_func)(self, "\n", 1) < 0)
  966.             return -1;
  967.  
  968.         Py_XDECREF(repr);
  969.     }
  970.     else {
  971.         int i;
  972.         char c_str[5];
  973.  
  974.         size = PyString_Size(args);
  975.  
  976.         if (size < 256) {
  977.             c_str[0] = SHORT_BINSTRING;
  978.             c_str[1] = size;
  979.             len = 2;
  980.         }
  981.         else {
  982.             c_str[0] = BINSTRING;
  983.             for (i = 1; i < 5; i++)
  984.                 c_str[i] = (int)(size >> ((i - 1) * 8));
  985.             len = 5;
  986.         }
  987.  
  988.         if ((*self->write_func)(self, c_str, len) < 0)
  989.             return -1;
  990.  
  991.         if ((*self->write_func)(self, 
  992.             PyString_AS_STRING((PyStringObject *)args), size) < 0)
  993.             return -1;
  994.     }
  995.  
  996.     if (doput)
  997.       if (put(self, args) < 0)
  998.     return -1;
  999.  
  1000.     return 0;
  1001. }
  1002.  
  1003.  
  1004. static int
  1005. save_tuple(Picklerobject *self, PyObject *args) {
  1006.     PyObject *element = 0, *py_tuple_id = 0;
  1007.     int len, i, has_key, res = -1;
  1008.  
  1009.     static char tuple = TUPLE;
  1010.  
  1011.     if ((*self->write_func)(self, &MARKv, 1) < 0)
  1012.         goto finally;
  1013.  
  1014.     if ((len = PyTuple_Size(args)) < 0)  
  1015.         goto finally;
  1016.  
  1017.     for (i = 0; i < len; i++) {
  1018.         UNLESS(element = PyTuple_GET_ITEM((PyTupleObject *)args, i))  
  1019.             goto finally;
  1020.     
  1021.         if (save(self, element, 0) < 0)
  1022.             goto finally;
  1023.     }
  1024.  
  1025.     UNLESS(py_tuple_id = PyInt_FromLong((long)args))
  1026.         goto finally;
  1027.  
  1028.     if (len) {
  1029.         if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_tuple_id)) < 0)
  1030.             goto finally;
  1031.  
  1032.         if (has_key) {
  1033.             if (self->bin) {
  1034.                 static char pop_mark = POP_MARK;
  1035.   
  1036.                 if ((*self->write_func)(self, &pop_mark, 1) < 0)
  1037.                     goto finally;
  1038.             }
  1039.             else {
  1040.                 static char pop = POP;
  1041.        
  1042.                 for (i = 0; i <= len; i++) {
  1043.                     if ((*self->write_func)(self, &pop, 1) < 0)
  1044.                         goto finally;
  1045.                 } 
  1046.             }
  1047.         
  1048.             if (get(self, py_tuple_id) < 0)
  1049.                 goto finally;
  1050.  
  1051.             res = 0;
  1052.             goto finally;
  1053.         }
  1054.     }
  1055.  
  1056.     if ((*self->write_func)(self, &tuple, 1) < 0) {
  1057.         goto finally;
  1058.     }
  1059.  
  1060.     if (put(self, args) < 0)
  1061.         goto finally;
  1062.  
  1063.     res = 0;
  1064.  
  1065. finally:
  1066.     Py_XDECREF(py_tuple_id);
  1067.  
  1068.     return res;
  1069. }
  1070.  
  1071. static int
  1072. save_empty_tuple(Picklerobject *self, PyObject *args) {
  1073.     static char tuple = EMPTY_TUPLE;
  1074.  
  1075.     return (*self->write_func)(self, &tuple, 1);
  1076. }
  1077.  
  1078.  
  1079. static int
  1080. save_list(Picklerobject *self, PyObject *args) {
  1081.     PyObject *element = 0;
  1082.     int s_len, len, i, using_appends, res = -1;
  1083.     char s[3];
  1084.  
  1085.     static char append = APPEND, appends = APPENDS;
  1086.  
  1087.     if(self->bin) {
  1088.         s[0] = EMPTY_LIST;
  1089.         s_len = 1;
  1090.     } 
  1091.     else {
  1092.         s[0] = MARK;
  1093.         s[1] = LIST;
  1094.         s_len = 2;
  1095.     }
  1096.  
  1097.     if ((len = PyList_Size(args)) < 0)
  1098.         goto finally;
  1099.  
  1100.     if ((*self->write_func)(self, s, s_len) < 0)
  1101.         goto finally;
  1102.  
  1103.     if (len == 0) {
  1104.         if (put(self, args) < 0)
  1105.             goto finally;
  1106.     }
  1107.     else {
  1108.         if (put2(self, args) < 0)
  1109.             goto finally;
  1110.     }
  1111.  
  1112.     if ((using_appends = (self->bin && (len > 1))))
  1113.         if ((*self->write_func)(self, &MARKv, 1) < 0)
  1114.             goto finally;
  1115.  
  1116.     for (i = 0; i < len; i++) {
  1117.         UNLESS(element = PyList_GET_ITEM((PyListObject *)args, i))  
  1118.             goto finally;
  1119.  
  1120.         if (save(self, element, 0) < 0)  
  1121.             goto finally;    
  1122.  
  1123.         if (!using_appends) {
  1124.             if ((*self->write_func)(self, &append, 1) < 0)
  1125.                 goto finally;
  1126.         }
  1127.     }
  1128.  
  1129.     if (using_appends) {
  1130.         if ((*self->write_func)(self, &appends, 1) < 0)
  1131.             goto finally;
  1132.     }
  1133.  
  1134.     res = 0;
  1135.  
  1136. finally:
  1137.  
  1138.     return res;
  1139. }
  1140.  
  1141.  
  1142. static int
  1143. save_dict(Picklerobject *self, PyObject *args) {
  1144.     PyObject *key = 0, *value = 0;
  1145.     int i, len, res = -1, using_setitems;
  1146.     char s[3];
  1147.  
  1148.     static char setitem = SETITEM, setitems = SETITEMS;
  1149.  
  1150.     if (self->bin) {
  1151.     s[0] = EMPTY_DICT;
  1152.     len = 1;
  1153.     }
  1154.     else {
  1155.         s[0] = MARK;
  1156.         s[1] = DICT;
  1157.         len = 2;
  1158.     }
  1159.  
  1160.     if ((*self->write_func)(self, s, len) < 0)
  1161.         goto finally;
  1162.  
  1163.     if ((len = PyDict_Size(args)) < 0)
  1164.         goto finally;
  1165.  
  1166.     if (len == 0) {
  1167.         if (put(self, args) < 0)
  1168.             goto finally;
  1169.     }
  1170.     else {
  1171.         if (put2(self, args) < 0)
  1172.             goto finally;
  1173.     }
  1174.  
  1175.     if ((using_setitems = (self->bin && (PyDict_Size(args) > 1))))
  1176.         if ((*self->write_func)(self, &MARKv, 1) < 0)
  1177.             goto finally;
  1178.  
  1179.     i = 0;
  1180.     while (PyDict_Next(args, &i, &key, &value)) {
  1181.         if (save(self, key, 0) < 0)
  1182.             goto finally;
  1183.  
  1184.         if (save(self, value, 0) < 0)
  1185.             goto finally;
  1186.  
  1187.         if (!using_setitems) {
  1188.             if ((*self->write_func)(self, &setitem, 1) < 0)
  1189.                 goto finally;
  1190.         }
  1191.     }
  1192.  
  1193.     if (using_setitems) {
  1194.         if ((*self->write_func)(self, &setitems, 1) < 0)
  1195.             goto finally;
  1196.     }
  1197.  
  1198.     res = 0;
  1199.  
  1200. finally:
  1201.  
  1202.     return res;
  1203. }
  1204.  
  1205.  
  1206. static int  
  1207. save_inst(Picklerobject *self, PyObject *args) {
  1208.     PyObject *class = 0, *module = 0, *name = 0, *state = 0, 
  1209.              *getinitargs_func = 0, *getstate_func = 0, *class_args = 0;
  1210.     char *module_str, *name_str;
  1211.     int module_size, name_size, res = -1;
  1212.  
  1213.     static char inst = INST, obj = OBJ, build = BUILD;
  1214.  
  1215.     if ((*self->write_func)(self, &MARKv, 1) < 0)
  1216.         goto finally;
  1217.  
  1218.     UNLESS(class = PyObject_GetAttr(args, __class___str))
  1219.         goto finally;
  1220.  
  1221.     if (self->bin) {
  1222.         if (save(self, class, 0) < 0)
  1223.             goto finally;
  1224.     }
  1225.  
  1226.     if ((getinitargs_func = PyObject_GetAttr(args, __getinitargs___str))) {
  1227.         PyObject *element = 0;
  1228.         int i, len;
  1229.  
  1230.         UNLESS(class_args = 
  1231.             PyObject_CallObject(getinitargs_func, empty_tuple))
  1232.             goto finally;
  1233.  
  1234.         if ((len = PyObject_Length(class_args)) < 0)  
  1235.             goto finally;
  1236.  
  1237.         for (i = 0; i < len; i++) {
  1238.             UNLESS(element = PySequence_GetItem(class_args, i)) 
  1239.                 goto finally;
  1240.  
  1241.             if (save(self, element, 0) < 0) {
  1242.                 Py_DECREF(element);
  1243.                 goto finally;
  1244.             }
  1245.  
  1246.             Py_DECREF(element);
  1247.         }
  1248.     }
  1249.     else {
  1250.         PyErr_Clear();
  1251.     }
  1252.  
  1253.     if (!self->bin) {
  1254.         UNLESS(name = ((PyClassObject *)class)->cl_name) {
  1255.             PyErr_SetString(PicklingError, "class has no name");
  1256.             goto finally;
  1257.         }
  1258.  
  1259.         UNLESS(module = whichmodule(self->class_map, class, name))
  1260.             goto finally;
  1261.     
  1262.         module_str = PyString_AS_STRING((PyStringObject *)module);
  1263.         module_size = PyString_Size(module);
  1264.         name_str   = PyString_AS_STRING((PyStringObject *)name);
  1265.         name_size = PyString_Size(name);
  1266.  
  1267.         if ((*self->write_func)(self, &inst, 1) < 0)
  1268.             goto finally;
  1269.  
  1270.         if ((*self->write_func)(self, module_str, module_size) < 0)
  1271.             goto finally;
  1272.  
  1273.         if ((*self->write_func)(self, "\n", 1) < 0)
  1274.             goto finally;
  1275.  
  1276.         if ((*self->write_func)(self, name_str, name_size) < 0)
  1277.             goto finally;
  1278.  
  1279.         if ((*self->write_func)(self, "\n", 1) < 0)
  1280.             goto finally;
  1281.     }
  1282.     else if ((*self->write_func)(self, &obj, 1) < 0) {
  1283.         goto finally;
  1284.     }
  1285.  
  1286.     if ((getstate_func = PyObject_GetAttr(args, __getstate___str))) {
  1287.         UNLESS(state = PyObject_CallObject(getstate_func, empty_tuple))
  1288.             goto finally;
  1289.     }
  1290.     else {
  1291.         PyErr_Clear();
  1292.  
  1293.         UNLESS(state = PyObject_GetAttr(args, __dict___str)) {
  1294.             PyErr_Clear();
  1295.             res = 0;
  1296.             goto finally;
  1297.         }
  1298.     }
  1299.  
  1300.     if (!PyDict_Check(state)) {
  1301.         if (put2(self, args) < 0)
  1302.             goto finally;
  1303.     }
  1304.     else {
  1305.         if (put(self, args) < 0)
  1306.             goto finally;
  1307.     }
  1308.   
  1309.     if (save(self, state, 0) < 0)
  1310.         goto finally;
  1311.  
  1312.     if ((*self->write_func)(self, &build, 1) < 0)
  1313.         goto finally;
  1314.  
  1315.     res = 0;
  1316.  
  1317. finally:
  1318.     Py_XDECREF(module);
  1319.     Py_XDECREF(class);
  1320.     Py_XDECREF(state);
  1321.     Py_XDECREF(getinitargs_func);
  1322.     Py_XDECREF(getstate_func);
  1323.     Py_XDECREF(class_args);
  1324.  
  1325.     return res;
  1326. }
  1327.  
  1328.  
  1329. static int
  1330. save_global(Picklerobject *self, PyObject *args, PyObject *name) {
  1331.     PyObject *global_name = 0, *module = 0;
  1332.     char *name_str, *module_str; 
  1333.     int module_size, name_size, res = -1;
  1334.  
  1335.     static char global = GLOBAL;
  1336.  
  1337.     if (name) {
  1338.         global_name = name;
  1339.         Py_INCREF(global_name);
  1340.     }
  1341.     else {
  1342.         UNLESS(global_name = PyObject_GetAttr(args, __name___str))
  1343.             goto finally;
  1344.     }
  1345.  
  1346.     UNLESS(module = whichmodule(self->class_map, args, global_name))
  1347.         goto finally;
  1348.  
  1349.     module_str = PyString_AS_STRING((PyStringObject *)module);
  1350.     module_size = PyString_Size(module);
  1351.     name_str   = PyString_AS_STRING((PyStringObject *)global_name);
  1352.     name_size = PyString_Size(global_name);
  1353.  
  1354.     if ((*self->write_func)(self, &global, 1) < 0)
  1355.         goto finally;
  1356.  
  1357.     if ((*self->write_func)(self, module_str, module_size) < 0)
  1358.         goto finally;
  1359.  
  1360.     if ((*self->write_func)(self, "\n", 1) < 0)
  1361.         goto finally;
  1362.  
  1363.     if ((*self->write_func)(self, name_str, name_size) < 0)
  1364.         goto finally;
  1365.  
  1366.     if ((*self->write_func)(self, "\n", 1) < 0)
  1367.         goto finally;
  1368.  
  1369.     if (put(self, args) < 0)
  1370.         goto finally;
  1371.  
  1372.     res = 0;
  1373.  
  1374. finally:
  1375.     Py_XDECREF(module);
  1376.     Py_XDECREF(global_name);
  1377.  
  1378.     return res;
  1379. }
  1380.  
  1381. static int
  1382. save_pers(Picklerobject *self, PyObject *args, PyObject *f) {
  1383.     PyObject *pid = 0;
  1384.     int size, res = -1;
  1385.  
  1386.     static char persid = PERSID, binpersid = BINPERSID;
  1387.  
  1388.     UNLESS(self->arg)
  1389.         UNLESS(self->arg = PyTuple_New(1))
  1390.             goto finally;
  1391.     
  1392.     Py_INCREF(args);      
  1393.     if (PyTuple_SetItem(self->arg, 0, args) < 0)
  1394.         goto finally;
  1395.     
  1396.     UNLESS(pid = PyObject_CallObject(f, self->arg))
  1397.         goto finally;
  1398.  
  1399.     if (pid != Py_None) {
  1400.         if (!self->bin) {
  1401.             if (!PyString_Check(pid)) {
  1402.                 PyErr_SetString(PicklingError, 
  1403.                     "persistent id must be string");
  1404.                 goto finally;
  1405.             }
  1406.  
  1407.             if ((*self->write_func)(self, &persid, 1) < 0)
  1408.                 goto finally;
  1409.  
  1410.             if ((size = PyString_Size(pid)) < 0)
  1411.                 goto finally;
  1412.  
  1413.             if ((*self->write_func)(self, 
  1414.                 PyString_AS_STRING((PyStringObject *)pid), size) < 0)
  1415.                 goto finally;
  1416.  
  1417.             if ((*self->write_func)(self, "\n", 1) < 0)
  1418.                 goto finally;
  1419.      
  1420.             res = 1;
  1421.             goto finally;
  1422.         }
  1423.         else if (save(self, pid, 1) >= 0) {
  1424.             if ((*self->write_func)(self, &binpersid, 1) < 0)
  1425.                 res = -1;
  1426.             else
  1427.                 res = 1;
  1428.         }
  1429.  
  1430.         goto finally;              
  1431.     }
  1432.  
  1433.     res = 0;
  1434.  
  1435. finally:
  1436.     Py_XDECREF(pid);
  1437.  
  1438.     return res;
  1439. }
  1440.  
  1441.  
  1442. static int 
  1443. save_reduce(Picklerobject *self, PyObject *callable,
  1444.             PyObject *tup, PyObject *state, PyObject *ob) {
  1445.     static char reduce = REDUCE, build = BUILD;
  1446.  
  1447.     if (save(self, callable, 0) < 0)
  1448.         return -1;
  1449.  
  1450.     if (save(self, tup, 0) < 0)
  1451.         return -1;
  1452.  
  1453.     if ((*self->write_func)(self, &reduce, 1) < 0)
  1454.         return -1;
  1455.  
  1456.     if (ob != NULL) {
  1457.         if (state && !PyDict_Check(state)) {
  1458.             if (put2(self, ob) < 0)
  1459.                 return -1;
  1460.         }
  1461.         else {
  1462.             if (put(self, ob) < 0)
  1463.                 return -1;
  1464.         }
  1465.     }
  1466.     
  1467.     if (state) {
  1468.         if (save(self, state, 0) < 0)
  1469.             return -1;
  1470.  
  1471.         if ((*self->write_func)(self, &build, 1) < 0)
  1472.             return -1;
  1473.     }
  1474.  
  1475.     return 0;
  1476. }
  1477.  
  1478. static int
  1479. save(Picklerobject *self, PyObject *args, int  pers_save) {
  1480.     PyTypeObject *type;
  1481.     PyObject *py_ob_id = 0, *__reduce__ = 0, *t = 0, *arg_tup = 0,
  1482.              *callable = 0, *state = 0;
  1483.     int res = -1, tmp, size;
  1484.  
  1485.     if (!pers_save && self->pers_func) {
  1486.         if ((tmp = save_pers(self, args, self->pers_func)) != 0) {
  1487.             res = tmp;
  1488.             goto finally;
  1489.         }
  1490.     }
  1491.  
  1492.     if (args == Py_None) {
  1493.         res = save_none(self, args);
  1494.         goto finally;
  1495.     }
  1496.  
  1497.     type = args->ob_type;
  1498.  
  1499.     switch (type->tp_name[0]) {
  1500.         case 'i':
  1501.             if (type == &PyInt_Type) {
  1502.                 res = save_int(self, args);
  1503.                 goto finally;
  1504.             }
  1505.             break;
  1506.  
  1507.         case 'l':
  1508.             if (type == &PyLong_Type) {
  1509.                 res = save_long(self, args);
  1510.                 goto finally;
  1511.             }
  1512.             break;
  1513.  
  1514.         case 'f':
  1515.             if (type == &PyFloat_Type) {
  1516.                 res = save_float(self, args);
  1517.                 goto finally;
  1518.             }
  1519.         break;
  1520.  
  1521.         case 't':
  1522.             if (type == &PyTuple_Type && PyTuple_Size(args)==0) {
  1523.             if(self->bin) res = save_empty_tuple(self, args);
  1524.             else          res = save_tuple(self, args);
  1525.                 goto finally;
  1526.             }
  1527.  
  1528.         case 's':
  1529.             if ((type == &PyString_Type) && (PyString_Size(args) < 2)) {
  1530.                 res = save_string(self, args, 0);
  1531.                 goto finally;
  1532.             }
  1533.     }
  1534.  
  1535.     if (args->ob_refcnt > 1) {
  1536.         long ob_id;
  1537.         int  has_key;
  1538.  
  1539.         ob_id = (long)args;
  1540.  
  1541.         UNLESS(py_ob_id = PyInt_FromLong(ob_id))
  1542.             goto finally;
  1543.  
  1544.         if ((has_key = cPickle_PyMapping_HasKey(self->memo, py_ob_id)) < 0)
  1545.             goto finally;
  1546.  
  1547.         if (has_key) {
  1548.             if (get(self, py_ob_id) < 0)
  1549.                 goto finally;
  1550.  
  1551.             res = 0;
  1552.             goto finally;
  1553.         }
  1554.     }
  1555.  
  1556.     switch (type->tp_name[0]) {
  1557.         case 's':
  1558.             if (type == &PyString_Type) {
  1559.                 res = save_string(self, args, 1);
  1560.                 goto finally;
  1561.             }
  1562.  
  1563.         case 't':
  1564.             if (type == &PyTuple_Type) {
  1565.                 res = save_tuple(self, args);
  1566.                 goto finally;
  1567.         }
  1568.  
  1569.         case 'l':
  1570.             if (type == &PyList_Type) {
  1571.                 res = save_list(self, args);
  1572.                 goto finally;
  1573.             }
  1574.  
  1575.         case 'd':
  1576.             if (type == &PyDict_Type) {
  1577.                 res = save_dict(self, args);
  1578.                 goto finally; 
  1579.             }
  1580.  
  1581.         case 'i':
  1582.             if (type == &PyInstance_Type) {
  1583.                 res = save_inst(self, args);
  1584.                 goto finally;
  1585.             }
  1586.  
  1587.         case 'c':
  1588.             if (type == &PyClass_Type) {
  1589.                 res = save_global(self, args, NULL);
  1590.                 goto finally;
  1591.             }
  1592.  
  1593.         case 'f':
  1594.             if (type == &PyFunction_Type) {
  1595.                 res = save_global(self, args, NULL);
  1596.                 goto finally;
  1597.             }
  1598.  
  1599.         case 'b':
  1600.             if (type == &PyCFunction_Type) {
  1601.                 res = save_global(self, args, NULL);
  1602.                 goto finally;
  1603.             }
  1604.     }
  1605.  
  1606.     if (!pers_save && self->inst_pers_func) {
  1607.         if ((tmp = save_pers(self, args, self->inst_pers_func)) != 0) {
  1608.             res = tmp;
  1609.             goto finally;
  1610.         }
  1611.     }
  1612.  
  1613.     if ((__reduce__ = PyDict_GetItem(dispatch_table, (PyObject *)type))) {
  1614.         Py_INCREF(__reduce__);
  1615.  
  1616.         UNLESS(self->arg)
  1617.             UNLESS(self->arg = PyTuple_New(1))
  1618.                 goto finally;
  1619.  
  1620.         Py_INCREF(args);
  1621.         if (PyTuple_SetItem(self->arg, 0, args) < 0)
  1622.             goto finally;
  1623.  
  1624.         UNLESS(t = PyObject_CallObject(__reduce__, self->arg))
  1625.             goto finally;
  1626.     }        
  1627.     else {
  1628.         PyErr_Clear();
  1629.  
  1630.         if ((__reduce__ = PyObject_GetAttr(args, __reduce___str))) {
  1631.             UNLESS(t = PyObject_CallObject(__reduce__, empty_tuple))
  1632.                 goto finally;
  1633.         }
  1634.         else {
  1635.             PyErr_Clear();
  1636.         }
  1637.     }
  1638.  
  1639.     if (t) {
  1640.         if (PyString_Check(t)) {
  1641.             res = save_global(self, args, t);
  1642.             goto finally;
  1643.         }
  1644.  
  1645.         if (!PyTuple_Check(t)) {
  1646.             cPickle_ErrFormat(PicklingError, "Value returned by %s must "
  1647.                 "be a tuple", "O", __reduce__);
  1648.             goto finally;
  1649.         }
  1650.  
  1651.         size = PyTuple_Size(t);
  1652.         
  1653.         if ((size != 3) && (size != 2)) {
  1654.             cPickle_ErrFormat(PicklingError, "tuple returned by %s must "     
  1655.                 "contain only two or three elements", "O", __reduce__);
  1656.                 goto finally;
  1657.         }
  1658.         
  1659.         callable = PyTuple_GET_ITEM(t, 0);
  1660.  
  1661.         arg_tup = PyTuple_GET_ITEM(t, 1);
  1662.  
  1663.         if (size > 2) {
  1664.             state = PyTuple_GET_ITEM(t, 2);
  1665.         }
  1666.  
  1667.         UNLESS(PyTuple_Check(arg_tup) || arg_tup==Py_None) {
  1668.             cPickle_ErrFormat(PicklingError, "Second element of tuple "
  1669.                 "returned by %s must be a tuple", "O", __reduce__);
  1670.             goto finally;
  1671.         }
  1672.  
  1673.         res = save_reduce(self, callable, arg_tup, state, args);
  1674.         goto finally;
  1675.     }
  1676.  
  1677.     /*
  1678.     if (PyObject_HasAttrString(args, "__class__")) {
  1679.         res = save_inst(self, args);
  1680.         goto finally;
  1681.     }
  1682.     */
  1683.  
  1684.     cPickle_ErrFormat(PicklingError, "Cannot pickle %s objects.", 
  1685.         "O", (PyObject *)type);
  1686.  
  1687. finally:
  1688.     Py_XDECREF(py_ob_id);
  1689.     Py_XDECREF(__reduce__);
  1690.     Py_XDECREF(t);
  1691.    
  1692.     return res;
  1693. }
  1694.  
  1695.  
  1696. static int
  1697. dump(Picklerobject *self, PyObject *args) {
  1698.     static char stop = STOP;
  1699.  
  1700.     if (save(self, args, 0) < 0)
  1701.         return -1;
  1702.  
  1703.     if ((*self->write_func)(self, &stop, 1) < 0)
  1704.         return -1;
  1705.  
  1706.     if ((*self->write_func)(self, NULL, 0) < 0)
  1707.         return -1;
  1708.  
  1709.     return 0;
  1710. }
  1711.  
  1712. static PyObject *
  1713. Pickler_dump(Picklerobject *self, PyObject *args) {
  1714.     PyObject *ob;
  1715.  
  1716.     UNLESS(PyArg_ParseTuple(args, "O", &ob))
  1717.         return NULL;
  1718.  
  1719.     if (dump(self, ob) < 0)
  1720.         return NULL;
  1721.  
  1722.     Py_INCREF(Py_None);
  1723.     return Py_None;
  1724. }
  1725.  
  1726.  
  1727. static PyObject *
  1728. dump_special(Picklerobject *self, PyObject *args) {
  1729.     static char stop = STOP;
  1730.  
  1731.     PyObject *callable, *arg_tup, *state = NULL;
  1732.     
  1733.     UNLESS(PyArg_ParseTuple(args, "OO|O", &callable, &arg_tup, &state))
  1734.         return NULL;
  1735.  
  1736.     UNLESS(PyTuple_Check(arg_tup)) {
  1737.         PyErr_SetString(PicklingError, "Second arg to dump_special must "
  1738.             "be tuple");
  1739.         return NULL;
  1740.     }
  1741.  
  1742.     if (save_reduce(self, callable, arg_tup, state, NULL) < 0)
  1743.         return NULL;
  1744.  
  1745.     if ((*self->write_func)(self, &stop, 1) < 0)
  1746.         return NULL;
  1747.  
  1748.     if ((*self->write_func)(self, NULL, 0) < 0)
  1749.         return NULL;
  1750.  
  1751.     Py_INCREF(Py_None);
  1752.     return Py_None;
  1753. }
  1754.  
  1755. static PyObject *
  1756. Pickle_clear_memo(Picklerobject *self, PyObject *args) {
  1757.   if(self->memo) PyDict_Clear(self->memo);
  1758.   Py_INCREF(Py_None);
  1759.   return Py_None;
  1760. }
  1761.  
  1762. static struct PyMethodDef Pickler_methods[] = {
  1763.   {"dump",          (PyCFunction)Pickler_dump,  1,
  1764.    "dump(object) --"
  1765.    "Write an object in pickle format to the object's pickle stream\n"
  1766.   },
  1767.   {"dump_special",  (PyCFunction)dump_special,  1,
  1768.    ""},
  1769.   {"clear_memo",  (PyCFunction)Pickle_clear_memo,  1,
  1770.    "clear_memo() -- Clear the picklers memo"},
  1771.   {NULL,                NULL}           /* sentinel */
  1772. };
  1773.  
  1774.  
  1775. static Picklerobject *
  1776. newPicklerobject(PyObject *file, int bin) {
  1777.     Picklerobject *self;
  1778.  
  1779.     UNLESS(self = PyObject_NEW(Picklerobject, &Picklertype))
  1780.         return NULL;
  1781.  
  1782.     self->fp = NULL;
  1783.     self->write = NULL;
  1784.     self->memo = NULL;
  1785.     self->arg = NULL;
  1786.     self->pers_func = NULL;
  1787.     self->inst_pers_func = NULL;
  1788.     self->write_buf = NULL;
  1789.     self->bin = bin;
  1790.     self->buf_size = 0;
  1791.     self->class_map = NULL;
  1792.     self->dispatch_table = NULL;
  1793.  
  1794.     Py_INCREF(file);
  1795.     self->file = file;
  1796.  
  1797.     UNLESS(self->memo = PyDict_New()) {
  1798.        Py_XDECREF((PyObject *)self);
  1799.        return NULL;
  1800.     }
  1801.  
  1802.     if (PyFile_Check(file)) {
  1803.         self->fp = PyFile_AsFile(file);
  1804.         self->write_func = write_file;
  1805.     }
  1806.     else if (PycStringIO_OutputCheck(file)) {
  1807.         self->write_func = write_cStringIO;
  1808.     }
  1809.     else if (file == Py_None) {
  1810.         self->write_func = write_none;
  1811.     }
  1812.     else {
  1813.         self->write_func = write_other;
  1814.  
  1815.         UNLESS(self->write = PyObject_GetAttr(file, write_str)) {
  1816.             PyErr_Clear();
  1817.             PyErr_SetString(PyExc_TypeError, "argument must have 'write' "
  1818.                 "attribute");
  1819.         goto err;
  1820.         }
  1821.  
  1822.         UNLESS(self->write_buf = 
  1823.             (char *)malloc(WRITE_BUF_SIZE * sizeof(char))) { 
  1824.             PyErr_NoMemory();
  1825.         goto err;
  1826.         }
  1827.     }
  1828.  
  1829.     if(PyEval_GetRestricted()) {
  1830.     /* Restricted execution, get private tables */
  1831.     PyObject *m;
  1832.  
  1833.     UNLESS(self->class_map=PyDict_New()) goto err;
  1834.     UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
  1835.     self->dispatch_table=PyObject_GetAttr(m, dispatch_table_str);
  1836.     Py_DECREF(m);
  1837.     UNLESS(self->dispatch_table) goto err;
  1838.     }
  1839.     else {
  1840.     self->class_map=class_map;
  1841.     Py_INCREF(class_map);
  1842.     self->dispatch_table=dispatch_table;
  1843.     Py_INCREF(dispatch_table);
  1844.     }
  1845.  
  1846.     return self;
  1847.  
  1848. err:
  1849.     Py_DECREF((PyObject *)self);
  1850.     return NULL;
  1851. }
  1852.  
  1853.  
  1854. static PyObject *
  1855. get_Pickler(PyObject *self, PyObject *args) {
  1856.     PyObject *file;
  1857.     int bin = 0;
  1858.  
  1859.     UNLESS(PyArg_ParseTuple(args, "O|i", &file, &bin))  return NULL;
  1860.     return (PyObject *)newPicklerobject(file, bin);
  1861. }
  1862.  
  1863.  
  1864. static void
  1865. Pickler_dealloc(Picklerobject *self) {
  1866.     Py_XDECREF(self->write);
  1867.     Py_XDECREF(self->memo);
  1868.     Py_XDECREF(self->arg);
  1869.     Py_XDECREF(self->file);
  1870.     Py_XDECREF(self->pers_func);
  1871.     Py_XDECREF(self->inst_pers_func);
  1872.     Py_XDECREF(self->class_map);
  1873.     Py_XDECREF(self->dispatch_table);
  1874.  
  1875.     if (self->write_buf) {    
  1876.         free(self->write_buf);
  1877.     }
  1878.  
  1879.     PyMem_DEL(self);
  1880. }
  1881.  
  1882.  
  1883. static PyObject *
  1884. Pickler_getattr(Picklerobject *self, char *name) {
  1885.     if (strcmp(name, "persistent_id") == 0) {
  1886.         if (!self->pers_func) {
  1887.             PyErr_SetString(PyExc_AttributeError, name);
  1888.             return NULL;
  1889.         }
  1890.  
  1891.         Py_INCREF(self->pers_func);
  1892.         return self->pers_func;
  1893.     }
  1894.  
  1895.     if (strcmp(name, "memo") == 0) {
  1896.         if (!self->memo) {
  1897.             PyErr_SetString(PyExc_AttributeError, name);
  1898.             return NULL;
  1899.         }
  1900.  
  1901.         Py_INCREF(self->memo);
  1902.         return self->memo;
  1903.     }
  1904.  
  1905.     if (strcmp(name, "PicklingError") == 0) {
  1906.         Py_INCREF(PicklingError);
  1907.         return PicklingError;
  1908.     }
  1909.   
  1910.     return Py_FindMethod(Pickler_methods, (PyObject *)self, name);
  1911. }
  1912.  
  1913.  
  1914. int 
  1915. Pickler_setattr(Picklerobject *self, char *name, PyObject *value) {
  1916.     if (strcmp(name, "persistent_id") == 0) {
  1917.         Py_XDECREF(self->pers_func);
  1918.         self->pers_func = value;
  1919.         Py_INCREF(value);
  1920.         return 0;
  1921.     }
  1922.  
  1923.     if (strcmp(name, "inst_persistent_id") == 0) {
  1924.         Py_XDECREF(self->inst_pers_func);
  1925.         self->inst_pers_func = value;
  1926.         Py_INCREF(value);
  1927.         return 0;
  1928.     }
  1929.  
  1930.     PyErr_SetString(PyExc_AttributeError, name);
  1931.     return -1;
  1932. }
  1933.  
  1934.  
  1935. static char Picklertype__doc__[] =
  1936. "Objects that know how to pickle objects\n"
  1937. ;
  1938.  
  1939. static PyTypeObject Picklertype = {
  1940.     PyObject_HEAD_INIT(NULL)
  1941.     0,                            /*ob_size*/
  1942.     "Pickler",                    /*tp_name*/
  1943.     sizeof(Picklerobject),                /*tp_basicsize*/
  1944.     0,                            /*tp_itemsize*/
  1945.     /* methods */
  1946.     (destructor)Pickler_dealloc,  /*tp_dealloc*/
  1947.     (printfunc)0,         /*tp_print*/
  1948.     (getattrfunc)Pickler_getattr, /*tp_getattr*/
  1949.     (setattrfunc)Pickler_setattr, /*tp_setattr*/
  1950.     (cmpfunc)0,           /*tp_compare*/
  1951.     (reprfunc)0,          /*tp_repr*/
  1952.     0,                    /*tp_as_number*/
  1953.     0,            /*tp_as_sequence*/
  1954.     0,            /*tp_as_mapping*/
  1955.     (hashfunc)0,          /*tp_hash*/
  1956.     (ternaryfunc)0,               /*tp_call*/
  1957.     (reprfunc)0,          /*tp_str*/
  1958.  
  1959.     /* Space for future expansion */
  1960.     0L,0L,0L,0L,
  1961.     Picklertype__doc__ /* Documentation string */
  1962. };
  1963.  
  1964. static PyObject *
  1965. find_class(PyObject *class_map,
  1966.        PyObject *py_module_name, PyObject *py_global_name) {
  1967.     PyObject *global = 0, *t = 0, *module;
  1968.  
  1969.     UNLESS(t = PyTuple_New(2)) return NULL;
  1970.  
  1971.     PyTuple_SET_ITEM((PyTupleObject *)t, 0, py_module_name);
  1972.     Py_INCREF(py_module_name);
  1973.     PyTuple_SET_ITEM((PyTupleObject *)t, 1, py_global_name);
  1974.     Py_INCREF(py_global_name);
  1975.  
  1976.     global=PyDict_GetItem(class_map, t);
  1977.  
  1978.     if (global) {
  1979.       Py_DECREF(t);
  1980.       Py_INCREF(global);
  1981.       return global;
  1982.     }
  1983.  
  1984.     PyErr_Clear();
  1985.  
  1986.     UNLESS(module=PyImport_Import(py_module_name)) return NULL;
  1987.     global=PyObject_GetAttr(module, py_global_name);
  1988.     Py_DECREF(module);
  1989.     UNLESS(global) return NULL;
  1990.  
  1991.     if (PyDict_SetItem(class_map, t, global) < 0) global=NULL;
  1992.     Py_DECREF(t);
  1993.  
  1994.     return global;
  1995. }
  1996.  
  1997.  
  1998. static int
  1999. marker(Unpicklerobject *self) {
  2000.     if (self->num_marks < 1) {
  2001.         PyErr_SetString(UnpicklingError, "could not find MARK");
  2002.         return -1;
  2003.     }
  2004.  
  2005.     return self->marks[--self->num_marks];
  2006. }
  2007.  
  2008.     
  2009. static int
  2010. load_none(Unpicklerobject *self) {
  2011.     if (PyList_Append(self->stack, Py_None) < 0)
  2012.         return -1;
  2013.  
  2014.     return 0;
  2015. }
  2016.  
  2017.  
  2018. static int
  2019. load_int(Unpicklerobject *self) {
  2020.     PyObject *py_int = 0;
  2021.     char *endptr, *s;
  2022.     int len, res = -1;
  2023.     long l;
  2024.  
  2025.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2026.     UNLESS(s=pystrndup(s,len)) return -1;
  2027.  
  2028.     errno = 0;
  2029.     l = strtol(s, &endptr, 0);
  2030.  
  2031.     if (errno || (*endptr != '\n') || (endptr[1] != '\0')) {
  2032.         /* Hm, maybe we've got something long.  Let's try reading
  2033.        it as a Python long object. */
  2034.         errno=0;
  2035.         UNLESS(py_int=PyLong_FromString(s,&endptr,0)) goto finally;
  2036.  
  2037.     if ((*endptr != '\n') || (endptr[1] != '\0')) {
  2038.         PyErr_SetString(PyExc_ValueError,
  2039.                 "could not convert string to int");
  2040.         goto finally;
  2041.     }
  2042.     }
  2043.     else {
  2044.         UNLESS(py_int = PyInt_FromLong(l)) goto finally;
  2045.     }
  2046.  
  2047.     if (PyList_Append(self->stack, py_int) < 0) goto finally;
  2048.  
  2049.     res = 0;
  2050.  
  2051. finally:
  2052.     free(s);
  2053.     Py_XDECREF(py_int);
  2054.  
  2055.     return res;
  2056. }
  2057.  
  2058.  
  2059. static long 
  2060. calc_binint(char *s, int  x) {
  2061.     unsigned char c;
  2062.     int i;
  2063.     long l;
  2064.  
  2065.     for (i = 0, l = 0L; i < x; i++) {
  2066.         c = (unsigned char)s[i];
  2067.         l |= (long)c << (i * 8);
  2068.     }
  2069.  
  2070.     return l;
  2071. }
  2072.  
  2073.  
  2074. static int
  2075. load_binintx(Unpicklerobject *self, char *s, int  x) {
  2076.     PyObject *py_int = 0;
  2077.     long l;
  2078.  
  2079.     l = calc_binint(s, x);
  2080.  
  2081.     UNLESS(py_int = PyInt_FromLong(l))
  2082.         return -1;
  2083.     
  2084.     if (PyList_Append(self->stack, py_int) < 0) {
  2085.         Py_DECREF(py_int);
  2086.         return -1;
  2087.     }
  2088.  
  2089.     Py_DECREF(py_int);
  2090.     
  2091.     return 0;
  2092. }
  2093.  
  2094.  
  2095. static int
  2096. load_binint(Unpicklerobject *self) {
  2097.     char *s;
  2098.  
  2099.     if ((*self->read_func)(self, &s, 4) < 0)
  2100.         return -1;
  2101.  
  2102.     return load_binintx(self, s, 4);
  2103. }
  2104.  
  2105.  
  2106. static int
  2107. load_binint1(Unpicklerobject *self) {
  2108.     char *s;
  2109.  
  2110.     if ((*self->read_func)(self, &s, 1) < 0)
  2111.         return -1;
  2112.  
  2113.     return load_binintx(self, s, 1);
  2114. }
  2115.  
  2116.  
  2117. static int
  2118. load_binint2(Unpicklerobject *self) {
  2119.     char *s;
  2120.  
  2121.     if ((*self->read_func)(self, &s, 2) < 0)
  2122.         return -1;
  2123.  
  2124.     return load_binintx(self, s, 2);
  2125. }
  2126.     
  2127. static int
  2128. load_long(Unpicklerobject *self) {
  2129.     PyObject *l = 0;
  2130.     char *end, *s;
  2131.     int len, res = -1;
  2132.  
  2133.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2134.     UNLESS(s=pystrndup(s,len)) return -1;
  2135.  
  2136.     UNLESS(l = PyLong_FromString(s, &end, 0))
  2137.         goto finally;
  2138.  
  2139.     if (PyList_Append(self->stack, l) < 0)
  2140.         goto finally;
  2141.  
  2142.     res = 0;
  2143.  
  2144. finally:
  2145.     free(s);
  2146.     Py_XDECREF(l);
  2147.  
  2148.     return res;
  2149. }
  2150.  
  2151.  
  2152. static int
  2153. load_float(Unpicklerobject *self) {
  2154.     PyObject *py_float = 0;
  2155.     char *endptr, *s;
  2156.     int len, res = -1;
  2157.     double d;
  2158.  
  2159.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2160.     UNLESS(s=pystrndup(s,len)) return -1;
  2161.  
  2162.     errno = 0;
  2163.     d = strtod(s, &endptr);
  2164.  
  2165.     if (errno || (endptr[0] != '\n') || (endptr[1] != '\0')) {
  2166.         PyErr_SetString(PyExc_ValueError, 
  2167.         "could not convert string to float");
  2168.         goto finally;
  2169.     }
  2170.  
  2171.     UNLESS(py_float = PyFloat_FromDouble(d))
  2172.         goto finally;
  2173.  
  2174.     if (PyList_Append(self->stack, py_float) < 0)
  2175.         goto finally;
  2176.  
  2177.     res = 0;
  2178.  
  2179. finally:
  2180.     free(s);
  2181.     Py_XDECREF(py_float);
  2182.  
  2183.     return res;
  2184. }
  2185.  
  2186. static int
  2187. load_binfloat(Unpicklerobject *self) {
  2188.     PyObject *py_float = 0;
  2189.     int s, e, res = -1;
  2190.     long fhi, flo;
  2191.     double x;
  2192.     char *p;
  2193.  
  2194.     if ((*self->read_func)(self, &p, 8) < 0)
  2195.         return -1;
  2196.  
  2197.     /* First byte */
  2198.     s = (*p>>7) & 1;
  2199.     e = (*p & 0x7F) << 4;
  2200.     p++;
  2201.  
  2202.     /* Second byte */
  2203.     e |= (*p>>4) & 0xF;
  2204.     fhi = (*p & 0xF) << 24;
  2205.     p++;
  2206.  
  2207.     /* Third byte */
  2208.     fhi |= (*p & 0xFF) << 16;
  2209.     p++;
  2210.  
  2211.     /* Fourth byte */
  2212.     fhi |= (*p & 0xFF) << 8;
  2213.     p++;
  2214.  
  2215.     /* Fifth byte */
  2216.     fhi |= *p & 0xFF;
  2217.     p++;
  2218.  
  2219.     /* Sixth byte */
  2220.     flo = (*p & 0xFF) << 16;
  2221.     p++;
  2222.  
  2223.     /* Seventh byte */
  2224.     flo |= (*p & 0xFF) << 8;
  2225.     p++;
  2226.  
  2227.     /* Eighth byte */
  2228.     flo |= *p & 0xFF;
  2229.  
  2230.     x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
  2231.     x /= 268435456.0; /* 2**28 */
  2232.  
  2233.     /* XXX This sadly ignores Inf/NaN */
  2234.     if (e == 0)
  2235.         e = -1022;
  2236.     else {
  2237.         x += 1.0;
  2238.         e -= 1023;
  2239.     }
  2240.     x = ldexp(x, e);
  2241.  
  2242.     if (s)
  2243.         x = -x;
  2244.  
  2245.     UNLESS(py_float = PyFloat_FromDouble(x))
  2246.         goto finally;
  2247.  
  2248.     if (PyList_Append(self->stack, py_float) < 0) 
  2249.         goto finally;
  2250.  
  2251.     res = 0;
  2252.  
  2253. finally:
  2254.     Py_XDECREF(py_float);
  2255.  
  2256.     return res;
  2257. }
  2258.  
  2259. static int
  2260. load_string(Unpicklerobject *self) {
  2261.     PyObject *str = 0;
  2262.     int len, res = -1, nslash;
  2263.     char *s, q, *p;
  2264.  
  2265.     static PyObject *eval_dict = 0;
  2266.  
  2267.     if ((len = (*self->readline_func)(self, &s)) < 0) return -1;
  2268.     UNLESS(s=pystrndup(s,len)) return -1;
  2269.  
  2270.     /* Check for unquoted quotes (evil strings) */
  2271.     q=*s;
  2272.     if(q != '"' && q != '\'') goto insecure;
  2273.     for(p=s+1, nslash=0; *p; p++)
  2274.       {
  2275.     if(*p==q && nslash%2==0) break;
  2276.     if(*p=='\\') nslash++;
  2277.     else nslash=0;
  2278.       }
  2279.     if(*p==q)
  2280.       {
  2281.     for(p++; *p; p++) if(*p > ' ') goto insecure;
  2282.       }
  2283.     else goto insecure;
  2284.     /********************************************/
  2285.  
  2286.     UNLESS(eval_dict)
  2287.         UNLESS(eval_dict = Py_BuildValue("{s{}}", "__builtins__"))
  2288.             goto finally;
  2289.  
  2290.     UNLESS(str = PyRun_String(s, Py_eval_input, eval_dict, eval_dict))
  2291.         goto finally;
  2292.  
  2293.     if (PyList_Append(self->stack, str) < 0)
  2294.         goto finally;
  2295.  
  2296.     res = 0;
  2297.  
  2298. finally:
  2299.     free(s);
  2300.     Py_XDECREF(str);
  2301.  
  2302.     return res;
  2303.     
  2304. insecure:
  2305.     free(s);
  2306.     PyErr_SetString(PyExc_ValueError,"insecure string pickle");
  2307.     return -1;
  2308.  
  2309.  
  2310. static int
  2311. load_binstring(Unpicklerobject *self) {
  2312.     PyObject *py_string = 0;
  2313.     long l;
  2314.     int res = -1;
  2315.     char *s;
  2316.  
  2317.     if ((*self->read_func)(self, &s, 4) < 0)
  2318.         goto finally;
  2319.  
  2320.     l = calc_binint(s, 4);
  2321.  
  2322.     if ((*self->read_func)(self, &s, l) < 0)
  2323.         goto finally;
  2324.  
  2325.     UNLESS(py_string = PyString_FromStringAndSize(s, l))
  2326.         goto finally;
  2327.  
  2328.     if (PyList_Append(self->stack, py_string) < 0)
  2329.         goto finally;
  2330.  
  2331.     res = 0;
  2332.  
  2333. finally:
  2334.     Py_XDECREF(py_string);
  2335.  
  2336.     return res;
  2337. }
  2338.  
  2339.  
  2340. static int
  2341. load_short_binstring(Unpicklerobject *self) {
  2342.     PyObject *py_string = 0;
  2343.     unsigned char l;  
  2344.     int res = -1;
  2345.     char *s;
  2346.  
  2347.     if ((*self->read_func)(self, &s, 1) < 0)
  2348.         return -1;
  2349.  
  2350.     l = (unsigned char)s[0];
  2351.  
  2352.     if ((*self->read_func)(self, &s, l) < 0)
  2353.         goto finally;
  2354.  
  2355.     UNLESS(py_string = PyString_FromStringAndSize(s, l))
  2356.         goto finally;
  2357.  
  2358.     if (PyList_Append(self->stack, py_string) < 0)
  2359.         goto finally;
  2360.  
  2361.     res = 0;
  2362.  
  2363. finally:
  2364.     Py_XDECREF(py_string);
  2365.  
  2366.     return res;
  2367.  
  2368.  
  2369. static int
  2370. load_tuple(Unpicklerobject *self) {
  2371.     PyObject *tup = 0, *slice = 0, *list = 0;
  2372.     int i, j, res = -1;
  2373.  
  2374.     if ((i = marker(self)) < 0)
  2375.         goto finally;
  2376.  
  2377.     if ((j = PyList_Size(self->stack)) < 0)  
  2378.         goto finally;
  2379.  
  2380.     UNLESS(slice = PyList_GetSlice(self->stack, i, j))
  2381.         goto finally;
  2382.   
  2383.     UNLESS(tup = PySequence_Tuple(slice))
  2384.         goto finally;
  2385.  
  2386.     UNLESS(list = PyList_New(1))
  2387.         goto finally;
  2388.  
  2389.     Py_INCREF(tup);
  2390.     if (PyList_SetItem(list, 0, tup) < 0)
  2391.         goto finally;
  2392.  
  2393.     if (PyList_SetSlice(self->stack, i, j, list) < 0)
  2394.         goto finally;
  2395.  
  2396.     res = 0;
  2397.  
  2398. finally:
  2399.     Py_XDECREF(tup);
  2400.     Py_XDECREF(list);
  2401.     Py_XDECREF(slice);
  2402.  
  2403.     return res;
  2404. }
  2405.  
  2406. static int
  2407. load_empty_tuple(Unpicklerobject *self) {
  2408.     PyObject *tup = 0;
  2409.     int res;
  2410.  
  2411.     UNLESS(tup=PyTuple_New(0)) return -1;
  2412.     res=PyList_Append(self->stack, tup);
  2413.     Py_DECREF(tup);
  2414.     return res;
  2415. }
  2416.  
  2417. static int
  2418. load_empty_list(Unpicklerobject *self) {
  2419.     PyObject *list = 0;
  2420.     int res;
  2421.  
  2422.     UNLESS(list=PyList_New(0)) return -1;
  2423.     res=PyList_Append(self->stack, list);
  2424.     Py_DECREF(list);
  2425.     return res;
  2426. }
  2427.  
  2428. static int
  2429. load_empty_dict(Unpicklerobject *self) {
  2430.     PyObject *dict = 0;
  2431.     int res;
  2432.  
  2433.     UNLESS(dict=PyDict_New()) return -1;
  2434.     res=PyList_Append(self->stack, dict);
  2435.     Py_DECREF(dict);
  2436.     return res;
  2437. }
  2438.  
  2439.  
  2440. static int
  2441. load_list(Unpicklerobject *self) {
  2442.     PyObject *list = 0, *slice = 0;
  2443.     int i, j, l, res = -1;
  2444.  
  2445.     if ((i = marker(self)) < 0)
  2446.         goto finally;
  2447.  
  2448.     if ((j = PyList_Size(self->stack)) < 0)
  2449.         goto finally;
  2450.  
  2451.     UNLESS(slice = PyList_GetSlice(self->stack, i, j))
  2452.         goto finally;
  2453.  
  2454.     if((l=PyList_Size(slice)) < 0)
  2455.         goto finally;
  2456.  
  2457.     if(l) {
  2458.       UNLESS(list = PyList_New(1))
  2459.         goto finally;
  2460.  
  2461.       Py_INCREF(slice);
  2462.       if (PyList_SetItem(list, 0, slice) < 0)
  2463.         goto finally;
  2464.       
  2465.       if (PyList_SetSlice(self->stack, i, j, list) < 0)
  2466.         goto finally;
  2467.     } else {
  2468.       if(PyList_Append(self->stack,slice) < 0)
  2469.     goto finally;
  2470.     }
  2471.  
  2472.     res = 0;
  2473.  
  2474. finally:
  2475.     Py_XDECREF(list);
  2476.     Py_XDECREF(slice);
  2477.  
  2478.     return res;
  2479. }
  2480.  
  2481. static int
  2482. load_dict(Unpicklerobject *self) {
  2483.     PyObject *list = 0, *dict = 0, *key = 0, *value = 0;
  2484.     int i, j, k, res = -1;
  2485.  
  2486.     if ((i = marker(self)) < 0)
  2487.         goto finally;
  2488.  
  2489.     if ((j = PyList_Size(self->stack)) < 0)
  2490.         goto finally;
  2491.  
  2492.     UNLESS(dict = PyDict_New())
  2493.         goto finally;
  2494.  
  2495.     for (k = i; k < j; k += 2) {
  2496.         UNLESS(key = PyList_GET_ITEM((PyListObject *)self->stack, k))
  2497.             goto finally;
  2498.  
  2499.         UNLESS(value = PyList_GET_ITEM((PyListObject *)self->stack, k + 1))
  2500.             goto finally;
  2501.  
  2502.         if (PyDict_SetItem(dict, key, value) < 0)
  2503.             goto finally;
  2504.     }
  2505.  
  2506.     if(j) {
  2507.  
  2508.       UNLESS(list = PyList_New(1))
  2509.         goto finally;
  2510.  
  2511.       Py_INCREF(dict);
  2512.       if (PyList_SetItem(list, 0, dict) < 0)
  2513.         goto finally;
  2514.  
  2515.       if (PyList_SetSlice(self->stack, i, j, list) < 0)
  2516.         goto finally;
  2517.     }
  2518.     else 
  2519.       if(PyList_Append(self->stack, dict) < 0)
  2520.     goto finally;
  2521.  
  2522.     res = 0;
  2523.  
  2524. finally:
  2525.     Py_XDECREF(dict);
  2526.     Py_XDECREF(list);
  2527.  
  2528.     return res;
  2529. }
  2530.  
  2531. static PyObject *
  2532. Instance_New(PyObject *cls, PyObject *args) {
  2533.   int has_key;
  2534.   PyObject *safe=0, *r=0;
  2535.  
  2536.   if (PyClass_Check(cls)) {
  2537.       int l;
  2538.       
  2539.       if((l=PyObject_Length(args)) < 0) goto err;
  2540.       UNLESS(l) {
  2541.       PyObject *__getinitargs__;
  2542.  
  2543.       UNLESS(__getinitargs__=PyObject_GetAttr(cls, __getinitargs___str)) {
  2544.           /* We have a class with no __getinitargs__, so bypass usual
  2545.          construction  */
  2546.           PyInstanceObject *inst;
  2547.  
  2548.           PyErr_Clear();
  2549.           UNLESS(inst=PyObject_NEW(PyInstanceObject, &PyInstance_Type))
  2550.         goto err;
  2551.           inst->in_class=(PyClassObject*)cls;
  2552.           Py_INCREF(cls);
  2553.           UNLESS(inst->in_dict=PyDict_New()) {
  2554.         Py_DECREF(inst);
  2555.         goto err;
  2556.           }
  2557.  
  2558.           return (PyObject *)inst;
  2559.         }
  2560.       Py_DECREF(__getinitargs__);
  2561.     }
  2562.       
  2563.       if((r=PyInstance_New(cls, args, NULL))) return r;
  2564.       else goto err;
  2565.     }
  2566.        
  2567.   
  2568.   if ((has_key = cPickle_PyMapping_HasKey(safe_constructors, cls)) < 0)
  2569.     goto err;
  2570.     
  2571.   if (!has_key)
  2572.     if(!(safe = PyObject_GetAttr(cls, __safe_for_unpickling___str)) ||
  2573.        !PyObject_IsTrue(safe)) {
  2574.       cPickle_ErrFormat(UnpicklingError, "%s is not safe for unpickling", "O", cls);
  2575.       Py_XDECREF(safe);
  2576.       return NULL;
  2577.   }
  2578.  
  2579.   if(args==Py_None)
  2580.     {
  2581.       /* Special case, call cls.__basicnew__() */
  2582.       PyObject *basicnew;
  2583.       
  2584.       UNLESS(basicnew=PyObject_GetAttr(cls, __basicnew___str)) return NULL;
  2585.       r=PyObject_CallObject(basicnew, NULL);
  2586.       Py_DECREF(basicnew);
  2587.       if(r) return r;
  2588.     }
  2589.  
  2590.   if((r=PyObject_CallObject(cls, args))) return r;
  2591.  
  2592. err:
  2593.   {
  2594.     PyObject *tp, *v, *tb;
  2595.  
  2596.     PyErr_Fetch(&tp, &v, &tb);
  2597.     if((r=Py_BuildValue("OOO",v,cls,args))) {
  2598.     Py_XDECREF(v);
  2599.     v=r;
  2600.       }
  2601.     PyErr_Restore(tp,v,tb);
  2602.   }
  2603.   return NULL;
  2604. }
  2605.   
  2606.  
  2607. static int
  2608. load_obj(Unpicklerobject *self) {
  2609.     PyObject *class = 0, *slice = 0, *tup = 0, *obj = 0;
  2610.     int i, len, res = -1;
  2611.  
  2612.     if ((i = marker(self)) < 0)
  2613.         goto finally;
  2614.  
  2615.     if ((len = PyList_Size(self->stack)) < 0)
  2616.         goto finally;
  2617.  
  2618.     UNLESS(slice = PyList_GetSlice(self->stack, i + 1, len))
  2619.         goto finally;
  2620.  
  2621.     UNLESS(tup = PySequence_Tuple(slice))
  2622.         goto finally;
  2623.  
  2624.     class = PyList_GET_ITEM((PyListObject *)self->stack, i);
  2625.     Py_INCREF(class);
  2626.  
  2627.     UNLESS(obj = Instance_New(class, tup))
  2628.         goto finally;
  2629.  
  2630.     if (DEL_LIST_SLICE(self->stack, i, len) < 0)
  2631.         goto finally;
  2632.  
  2633.     if (PyList_Append(self->stack, obj) < 0)
  2634.         goto finally;
  2635.   
  2636.     res = 0;
  2637.  
  2638. finally:
  2639.     
  2640.     Py_XDECREF(class);
  2641.     Py_XDECREF(slice);
  2642.     Py_XDECREF(tup);
  2643.     Py_XDECREF(obj);
  2644.  
  2645.     return res;
  2646. }
  2647.  
  2648.  
  2649. static int
  2650. load_inst(Unpicklerobject *self) {
  2651.     PyObject *arg_tup = 0, *arg_slice = 0, *class = 0, *obj = 0,
  2652.              *module_name = 0, *class_name = 0;
  2653.     int i, j, len, res = -1;
  2654.     char *s;
  2655.  
  2656.     if ((i = marker(self)) < 0) goto finally;
  2657.  
  2658.     if ((j = PyList_Size(self->stack)) < 0) goto finally;
  2659.  
  2660.     UNLESS(arg_slice = PyList_GetSlice(self->stack, i, j)) goto finally;
  2661.  
  2662.     UNLESS(arg_tup = PySequence_Tuple(arg_slice)) goto finally;
  2663.  
  2664.     if (DEL_LIST_SLICE(self->stack, i, j) < 0) goto finally;
  2665.  
  2666.     if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
  2667.  
  2668.     UNLESS(module_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
  2669.  
  2670.     if ((len = (*self->readline_func)(self, &s)) < 0) goto finally;
  2671.  
  2672.     UNLESS(class_name = PyString_FromStringAndSize(s, len - 1)) goto finally;
  2673.  
  2674.     UNLESS(class = find_class(self->class_map, module_name, class_name))
  2675.         goto finally;
  2676.  
  2677.     UNLESS(obj = Instance_New(class, arg_tup)) goto finally;
  2678.  
  2679.     if (PyList_Append(self->stack, obj) < 0) goto finally;
  2680.  
  2681.     res = 0;
  2682.  
  2683. finally:
  2684.     Py_XDECREF(class);
  2685.     Py_XDECREF(arg_slice);
  2686.     Py_XDECREF(arg_tup);
  2687.     Py_XDECREF(obj);
  2688.     Py_XDECREF(module_name);
  2689.     Py_XDECREF(class_name);
  2690.  
  2691.     return res;
  2692. }
  2693.  
  2694.  
  2695. static int
  2696. load_global(Unpicklerobject *self) {
  2697.     PyObject *class = 0, *module_name = 0, *class_name = 0;
  2698.     int res = -1, len;
  2699.     char *s;
  2700.  
  2701.     if ((len = (*self->readline_func)(self, &s)) < 0)
  2702.         goto finally;
  2703.  
  2704.     UNLESS(module_name = PyString_FromStringAndSize(s, len - 1))
  2705.         goto finally;
  2706.  
  2707.     if ((len = (*self->readline_func)(self, &s)) < 0)
  2708.         goto finally;
  2709.  
  2710.     UNLESS(class_name = PyString_FromStringAndSize(s, len - 1))
  2711.         goto finally;
  2712.  
  2713.     UNLESS(class = find_class(self->class_map, module_name, class_name))
  2714.         goto finally;
  2715.  
  2716.     if (PyList_Append(self->stack, class) < 0)
  2717.         goto finally;
  2718.  
  2719.     res = 0;
  2720.  
  2721. finally:
  2722.     Py_XDECREF(class);
  2723.     Py_XDECREF(module_name);
  2724.     Py_XDECREF(class_name);
  2725.  
  2726.     return res;
  2727. }
  2728.  
  2729.  
  2730. static int
  2731. load_persid(Unpicklerobject *self) {
  2732.     PyObject *pid = 0, *pers_load_val = 0;
  2733.     int len, res = -1;
  2734.     char *s;
  2735.  
  2736.     if (self->pers_func) {
  2737.         if ((len = (*self->readline_func)(self, &s)) < 0)
  2738.             goto finally;
  2739.   
  2740.         UNLESS(pid = PyString_FromStringAndSize(s, len - 1))
  2741.            goto finally;
  2742.  
  2743.     if(PyList_Check(self->pers_func)) {
  2744.         if(PyList_Append(self->pers_func, pid) < 0) goto finally;
  2745.         pers_load_val=pid;
  2746.         Py_INCREF(pid);
  2747.       }
  2748.     else {
  2749.         UNLESS(self->arg)
  2750.           UNLESS(self->arg = PyTuple_New(1))
  2751.             goto finally;
  2752.  
  2753.         Py_INCREF(pid);
  2754.         if (PyTuple_SetItem(self->arg, 0, pid) < 0)
  2755.           goto finally;
  2756.       
  2757.         UNLESS(pers_load_val = 
  2758.            PyObject_CallObject(self->pers_func, self->arg))
  2759.           goto finally;
  2760.       }
  2761.     if (PyList_Append(self->stack, pers_load_val) < 0)
  2762.       goto finally;
  2763.     }
  2764.     else {
  2765.       PyErr_SetString(UnpicklingError,
  2766.               "A load persistent id instruction was encountered,\n"
  2767.               "but no persistent_load function was specified.");
  2768.       goto finally;
  2769.     }
  2770.  
  2771.     res = 0;
  2772.  
  2773. finally:
  2774.     Py_XDECREF(pid);
  2775.     Py_XDECREF(pers_load_val);
  2776.  
  2777.     return res;
  2778. }
  2779.  
  2780.  
  2781. static int
  2782. load_binpersid(Unpicklerobject *self) {
  2783.     PyObject *pid = 0, *pers_load_val = 0;
  2784.     int len, res = -1;
  2785.  
  2786.     if (self->pers_func) {
  2787.         if ((len = PyList_Size(self->stack)) < 0)
  2788.             goto finally;
  2789.  
  2790.         pid = PyList_GET_ITEM((PyListObject *)self->stack, len - 1);
  2791.         Py_INCREF(pid);
  2792.  
  2793.         if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
  2794.             goto finally;
  2795.  
  2796.     if(PyList_Check(self->pers_func)) {
  2797.         if(PyList_Append(self->pers_func, pid) < 0) goto finally;
  2798.         pers_load_val=pid;
  2799.         Py_INCREF(pid);
  2800.       }
  2801.     else {
  2802.       UNLESS(self->arg)
  2803.             UNLESS(self->arg = PyTuple_New(1))
  2804.         goto finally;
  2805.  
  2806.       Py_INCREF(pid);
  2807.       if (PyTuple_SetItem(self->arg, 0, pid) < 0)
  2808.             goto finally;
  2809.  
  2810.       UNLESS(pers_load_val = 
  2811.          PyObject_CallObject(self->pers_func, self->arg))
  2812.             goto finally;
  2813.     }
  2814.         if (PyList_Append(self->stack, pers_load_val) < 0)
  2815.             goto finally;
  2816.     }
  2817.     else {
  2818.       PyErr_SetString(UnpicklingError,
  2819.               "A load persistent id instruction was encountered,\n"
  2820.               "but no persistent_load function was specified.");
  2821.       goto finally;
  2822.     }
  2823.  
  2824.     res = 0;
  2825.  
  2826. finally:
  2827.     Py_XDECREF(pid);
  2828.     Py_XDECREF(pers_load_val);
  2829.  
  2830.     return res;
  2831. }
  2832.  
  2833.  
  2834. static int
  2835. load_pop(Unpicklerobject *self) {
  2836.     int len;
  2837.  
  2838.     if ((len = PyList_Size(self->stack)) < 0)  
  2839.         return -1;
  2840.  
  2841.     if ((self->num_marks > 0) && 
  2842.         (self->marks[self->num_marks - 1] == len))
  2843.         self->num_marks--;
  2844.     else if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)  
  2845.         return -1;
  2846.  
  2847.     return 0;
  2848. }
  2849.  
  2850.  
  2851. static int
  2852. load_pop_mark(Unpicklerobject *self) {
  2853.     int i, len;
  2854.  
  2855.     if ((i = marker(self)) < 0)
  2856.         return -1;
  2857.  
  2858.     if ((len = PyList_Size(self->stack)) < 0)
  2859.         return -1;
  2860.  
  2861.     if (DEL_LIST_SLICE(self->stack, i, len) < 0)
  2862.         return -1;
  2863.  
  2864.     return 0;
  2865. }
  2866.  
  2867.  
  2868. static int
  2869. load_dup(Unpicklerobject *self) {
  2870.     PyObject *last;
  2871.     int len;
  2872.  
  2873.     if ((len = PyList_Size(self->stack)) < 0)
  2874.         return -1;
  2875.   
  2876.     UNLESS(last = PyList_GetItem(self->stack, len - 1))  
  2877.         return -1;
  2878.  
  2879.     if (PyList_Append(self->stack, last) < 0)
  2880.         return -1;
  2881.  
  2882.     return 0;
  2883. }
  2884.  
  2885.  
  2886. static int
  2887. load_get(Unpicklerobject *self) {
  2888.     PyObject *py_str = 0, *value = 0;
  2889.     int len, res = -1;
  2890.     char *s;
  2891.  
  2892.     if ((len = (*self->readline_func)(self, &s)) < 0)
  2893.         goto finally;
  2894.  
  2895.     UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
  2896.         goto finally;
  2897.   
  2898.     UNLESS(value = PyDict_GetItem(self->memo, py_str))  
  2899.         goto finally;
  2900.  
  2901.     if (PyList_Append(self->stack, value) < 0)
  2902.         goto finally;
  2903.  
  2904.     res = 0;
  2905.  
  2906. finally:
  2907.     Py_XDECREF(py_str);
  2908.  
  2909.     return res;
  2910. }
  2911.  
  2912.  
  2913. static int
  2914. load_binget(Unpicklerobject *self) {
  2915.     PyObject *py_key = 0, *value = 0;
  2916.     unsigned char key;
  2917.     int res = -1;
  2918.     char *s;
  2919.  
  2920.     if ((*self->read_func)(self, &s, 1) < 0)
  2921.         goto finally;
  2922.  
  2923.     key = (unsigned char)s[0];
  2924.  
  2925.     UNLESS(py_key = PyInt_FromLong((long)key))
  2926.         goto finally;
  2927.  
  2928.     UNLESS(value = PyDict_GetItem(self->memo, py_key))  
  2929.         goto finally;
  2930.  
  2931.     if (PyList_Append(self->stack, value) < 0)
  2932.         goto finally;
  2933.  
  2934.     res = 0;
  2935.  
  2936. finally:
  2937.     Py_XDECREF(py_key);
  2938.  
  2939.     return res;
  2940. }
  2941.  
  2942.  
  2943. static int
  2944. load_long_binget(Unpicklerobject *self) {
  2945.     PyObject *py_key = 0, *value = 0;
  2946.     unsigned char c, *s;
  2947.     long key;
  2948.     int res = -1;
  2949.  
  2950.     if ((*self->read_func)(self, &s, 4) < 0)
  2951.         goto finally;
  2952.  
  2953.     c = (unsigned char)s[0];
  2954.     key = (long)c;
  2955.     c = (unsigned char)s[1];
  2956.     key |= (long)c << 8;
  2957.     c = (unsigned char)s[2];
  2958.     key |= (long)c << 16;
  2959.     c = (unsigned char)s[3];
  2960.     key |= (long)c << 24;
  2961.  
  2962.     UNLESS(py_key = PyInt_FromLong(key))
  2963.         goto finally;
  2964.  
  2965.     UNLESS(value = PyDict_GetItem(self->memo, py_key))  
  2966.         goto finally;
  2967.  
  2968.     if (PyList_Append(self->stack, value) < 0)
  2969.         goto finally;
  2970.  
  2971.     res = 0;
  2972.  
  2973. finally:
  2974.     Py_XDECREF(py_key);
  2975.  
  2976.     return res;
  2977. }
  2978.  
  2979.  
  2980. static int
  2981. load_put(Unpicklerobject *self) {
  2982.     PyObject *py_str = 0, *value = 0;
  2983.     int len, res = -1;
  2984.     char *s;
  2985.  
  2986.     if ((len = (*self->readline_func)(self, &s)) < 0)
  2987.         goto finally;
  2988.  
  2989.     UNLESS(py_str = PyString_FromStringAndSize(s, len - 1))
  2990.         goto finally;
  2991.  
  2992.     if ((len = PyList_Size(self->stack)) < 0)
  2993.         goto finally;
  2994.  
  2995.     UNLESS(value = PyList_GetItem(self->stack, len - 1))  
  2996.         goto finally;
  2997.  
  2998.     if (PyDict_SetItem(self->memo, py_str, value) < 0)  
  2999.         goto finally;
  3000.  
  3001.     res = 0;
  3002.  
  3003. finally:
  3004.     Py_XDECREF(py_str);
  3005.  
  3006.     return res;
  3007. }
  3008.  
  3009.  
  3010. static int
  3011. load_binput(Unpicklerobject *self) {
  3012.     PyObject *py_key = 0, *value = 0;
  3013.     unsigned char key, *s;
  3014.     int len, res = -1;
  3015.  
  3016.     if ((*self->read_func)(self, &s, 1) < 0)
  3017.         goto finally;
  3018.  
  3019.     key = (unsigned char)s[0];
  3020.  
  3021.     UNLESS(py_key = PyInt_FromLong((long)key))
  3022.         goto finally;
  3023.  
  3024.     if ((len = PyList_Size(self->stack)) < 0)
  3025.         goto finally;
  3026.  
  3027.     UNLESS(value = PyList_GetItem(self->stack, len - 1))  
  3028.         goto finally;
  3029.  
  3030.     if (PyDict_SetItem(self->memo, py_key, value) < 0)  
  3031.         goto finally;
  3032.  
  3033.     res = 0;
  3034.  
  3035. finally:
  3036.     Py_XDECREF(py_key);
  3037.  
  3038.     return res;
  3039. }
  3040.  
  3041.  
  3042. static int
  3043. load_long_binput(Unpicklerobject *self) {
  3044.     PyObject *py_key = 0, *value = 0;
  3045.     long key;
  3046.     unsigned char c, *s;
  3047.     int len, res = -1;
  3048.  
  3049.     if ((*self->read_func)(self, &s, 4) < 0)
  3050.         goto finally;
  3051.  
  3052.     c = (unsigned char)s[0];
  3053.     key = (long)c;
  3054.     c = (unsigned char)s[1];
  3055.     key |= (long)c << 8;
  3056.     c = (unsigned char)s[2];
  3057.     key |= (long)c << 16;
  3058.     c = (unsigned char)s[3];
  3059.     key |= (long)c << 24;
  3060.  
  3061.     UNLESS(py_key = PyInt_FromLong(key))
  3062.         goto finally;
  3063.  
  3064.     if ((len = PyList_Size(self->stack)) < 0)
  3065.         goto finally;
  3066.  
  3067.     UNLESS(value = PyList_GetItem(self->stack, len - 1))  
  3068.         goto finally;
  3069.  
  3070.     if (PyDict_SetItem(self->memo, py_key, value) < 0)  
  3071.         goto finally;
  3072.  
  3073.     res = 0;
  3074.  
  3075. finally:
  3076.     Py_XDECREF(py_key);
  3077.  
  3078.     return res;
  3079. }
  3080.  
  3081.  
  3082. static int 
  3083. do_append(Unpicklerobject *self, int  x) {
  3084.     PyObject *value = 0, *list = 0, *append_method = 0;
  3085.     int len, i;
  3086.  
  3087.     if ((len = PyList_Size(self->stack)) < 0)  
  3088.         return -1;
  3089.  
  3090.     UNLESS(list = PyList_GetItem(self->stack, x - 1))  
  3091.         goto err;
  3092.  
  3093.     if (PyList_Check(list)) {
  3094.         PyObject *slice = 0;
  3095.         int list_len;
  3096.        
  3097.         UNLESS(slice = PyList_GetSlice(self->stack, x, len))
  3098.             return -1;
  3099.   
  3100.         list_len = PyList_Size(list);
  3101.         if (PyList_SetSlice(list, list_len, list_len, slice) < 0) {
  3102.             Py_DECREF(slice);
  3103.             return -1;
  3104.         }
  3105.  
  3106.         Py_DECREF(slice);
  3107.     }
  3108.     else {
  3109.  
  3110.         UNLESS(append_method = PyObject_GetAttr(list, append_str))
  3111.             return -1;
  3112.          
  3113.         for (i = x; i < len; i++) {
  3114.         PyObject *junk;
  3115.  
  3116.             UNLESS(value = PyList_GetItem(self->stack, i))  
  3117.                 return -1;
  3118.  
  3119.         UNLESS(self->arg)
  3120.           UNLESS(self->arg = PyTuple_New(1)) 
  3121.             goto err;
  3122.         
  3123.         Py_INCREF(value);
  3124.         if (PyTuple_SetItem(self->arg, 0, value) < 0) 
  3125.           goto err;
  3126.         
  3127.         UNLESS(junk = PyObject_CallObject(append_method, self->arg)) 
  3128.           goto err;
  3129.         Py_DECREF(junk);
  3130.         }
  3131.     }
  3132.  
  3133.     if (DEL_LIST_SLICE(self->stack, x, len) < 0)  
  3134.         goto err;
  3135.  
  3136.     Py_XDECREF(append_method);
  3137.  
  3138.     return 0;
  3139.  
  3140. err:
  3141.     Py_XDECREF(append_method);
  3142.  
  3143.     return -1;
  3144. }
  3145.  
  3146.     
  3147. static int
  3148. load_append(Unpicklerobject *self) {
  3149.     return do_append(self, PyList_Size(self->stack) - 1);
  3150. }
  3151.  
  3152.  
  3153. static int
  3154. load_appends(Unpicklerobject *self) {
  3155.     return do_append(self, marker(self));
  3156. }
  3157.  
  3158.  
  3159. static int
  3160. do_setitems(Unpicklerobject *self, int  x) {
  3161.     PyObject *value = 0, *key = 0, *dict = 0;
  3162.     int len, i, res = -1;
  3163.  
  3164.     if ((len = PyList_Size(self->stack)) < 0)  
  3165.         goto finally;
  3166.  
  3167.     UNLESS(dict = PyList_GetItem(self->stack, x - 1))
  3168.         goto finally;
  3169.  
  3170.     for (i = x; i < len; i += 2) {
  3171.         UNLESS(key = PyList_GetItem(self->stack, i))  
  3172.             goto finally;
  3173.  
  3174.         UNLESS(value = PyList_GetItem(self->stack, i + 1))
  3175.             goto finally;
  3176.  
  3177.         if (PyObject_SetItem(dict, key, value) < 0)  
  3178.             goto finally;
  3179.     }
  3180.  
  3181.     if (DEL_LIST_SLICE(self->stack, x, len) < 0)  
  3182.         goto finally;
  3183.  
  3184.     res = 0;
  3185.  
  3186. finally:
  3187.  
  3188.     return res;
  3189. }
  3190.  
  3191.  
  3192. static int
  3193. load_setitem(Unpicklerobject *self) {
  3194.     return do_setitems(self, PyList_Size(self->stack) - 2);
  3195. }
  3196.  
  3197.  
  3198. static int
  3199. load_setitems(Unpicklerobject *self) {
  3200.     return do_setitems(self, marker(self));
  3201. }
  3202.  
  3203.  
  3204. static int
  3205. load_build(Unpicklerobject *self) {
  3206.     PyObject *value = 0, *inst = 0, *instdict = 0, *d_key = 0, *d_value = 0, 
  3207.              *junk = 0, *__setstate__ = 0;
  3208.     int len, i, res = -1;
  3209.  
  3210.     if ((len = PyList_Size(self->stack)) < 0)
  3211.         goto finally;
  3212.  
  3213.     UNLESS(value = PyList_GetItem(self->stack, len - 1))
  3214.         goto finally; 
  3215.     Py_INCREF(value);
  3216.  
  3217.     if (DEL_LIST_SLICE(self->stack, len - 1, len) < 0)
  3218.         goto finally;
  3219.  
  3220.     UNLESS(inst = PyList_GetItem(self->stack, len - 2))
  3221.         goto finally;
  3222.  
  3223.     UNLESS(__setstate__ = PyObject_GetAttr(inst, __setstate___str)) {
  3224.         PyErr_Clear();
  3225.  
  3226.         UNLESS(instdict = PyObject_GetAttr(inst, __dict___str))
  3227.             goto finally;
  3228.  
  3229.         i = 0;
  3230.         while (PyDict_Next(value, &i, &d_key, &d_value)) {
  3231.             if (PyObject_SetItem(instdict, d_key, d_value) < 0)
  3232.                 goto finally;
  3233.         }
  3234.     }
  3235.     else {
  3236.         UNLESS(self->arg)
  3237.             UNLESS(self->arg = PyTuple_New(1))
  3238.                 goto finally;
  3239.  
  3240.         Py_INCREF(value);
  3241.         if (PyTuple_SetItem(self->arg, 0, value) < 0)
  3242.             goto finally;
  3243.  
  3244.         UNLESS(junk = PyObject_CallObject(__setstate__, self->arg))
  3245.             goto finally;
  3246.         Py_DECREF(junk);
  3247.     }
  3248.  
  3249.     res = 0;
  3250.  
  3251. finally:
  3252.   Py_XDECREF(value);
  3253.   Py_XDECREF(instdict);
  3254.   Py_XDECREF(__setstate__);
  3255.   
  3256.   return res;
  3257. }
  3258.  
  3259.  
  3260. static int
  3261. load_mark(Unpicklerobject *self) {
  3262.     int len;
  3263.  
  3264.     if ((len = PyList_Size(self->stack)) < 0)
  3265.         return -1;
  3266.  
  3267.     if (!self->marks_size) {
  3268.         self->marks_size = 20;
  3269.         UNLESS(self->marks = (int *)malloc(self->marks_size * sizeof(int))) {
  3270.             PyErr_NoMemory();
  3271.             return -1;
  3272.         } 
  3273.     }
  3274.     else if ((self->num_marks + 1) >= self->marks_size) {
  3275.         UNLESS(self->marks = (int *)realloc(self->marks,
  3276.             (self->marks_size + 20) * sizeof(int))) {
  3277.             PyErr_NoMemory();
  3278.             return -1;
  3279.         }
  3280.  
  3281.         self->marks_size += 20;
  3282.     }
  3283.  
  3284.     self->marks[self->num_marks++] = len;
  3285.  
  3286.     return 0;
  3287. }
  3288.  
  3289. static int
  3290. load_reduce(Unpicklerobject *self) {
  3291.     PyObject *callable = 0, *arg_tup = 0, *ob = 0;
  3292.     int len, res = -1;
  3293.  
  3294.     if ((len = PyList_Size(self->stack)) < 0)
  3295.         goto finally;
  3296.  
  3297.     UNLESS(arg_tup = PyList_GetItem(self->stack, len - 1))
  3298.         goto finally;
  3299.  
  3300.     UNLESS(callable = PyList_GetItem(self->stack, len - 2))
  3301.         goto finally;
  3302.  
  3303.     UNLESS(ob = Instance_New(callable, arg_tup))
  3304.         goto finally;
  3305.  
  3306.     if (PyList_Append(self->stack, ob) < 0)
  3307.         goto finally;
  3308.  
  3309.     if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0)
  3310.         goto finally;
  3311.  
  3312.     res = 0;
  3313.  
  3314. finally:
  3315.     Py_XDECREF(ob);
  3316.  
  3317.     return res;
  3318. }
  3319.     
  3320. static PyObject *
  3321. load(Unpicklerobject *self) {
  3322.     PyObject *stack = 0, *err = 0, *val = 0;
  3323.     int len;
  3324.     char *s;
  3325.  
  3326.     UNLESS(stack = PyList_New(0))
  3327.         goto err;
  3328.  
  3329.     self->stack = stack;
  3330.     self->num_marks = 0;
  3331.  
  3332.     while (1) {
  3333.         if ((*self->read_func)(self, &s, 1) < 0)
  3334.             break;
  3335.  
  3336.         switch (s[0]) {
  3337.             case NONE:
  3338.                 if (load_none(self) < 0)
  3339.                     break;
  3340.                 continue;
  3341.  
  3342.             case BININT:
  3343.                  if (load_binint(self) < 0)
  3344.                      break;
  3345.                  continue;
  3346.  
  3347.             case BININT1:
  3348.                 if (load_binint1(self) < 0)
  3349.                     break;
  3350.                 continue;
  3351.  
  3352.             case BININT2:
  3353.                 if (load_binint2(self) < 0)
  3354.                     break;
  3355.                 continue;
  3356.  
  3357.             case INT:
  3358.                 if (load_int(self) < 0)
  3359.                     break;
  3360.                 continue;
  3361.  
  3362.             case LONG:
  3363.                 if (load_long(self) < 0)
  3364.                     break;
  3365.                 continue;
  3366.  
  3367.             case FLOAT:
  3368.                 if (load_float(self) < 0)
  3369.                     break;
  3370.                 continue;
  3371.  
  3372. #ifdef FORMAT_1_3
  3373.             case BINFLOAT:
  3374.                 if (load_binfloat(self) < 0)
  3375.                     break;
  3376.                 continue;
  3377. #endif
  3378.  
  3379.             case BINSTRING:
  3380.                 if (load_binstring(self) < 0)
  3381.                     break;
  3382.                 continue;
  3383.  
  3384.             case SHORT_BINSTRING:
  3385.                 if (load_short_binstring(self) < 0)
  3386.                     break;
  3387.                 continue;
  3388.  
  3389.             case STRING:
  3390.                 if (load_string(self) < 0)
  3391.                     break;
  3392.                 continue;
  3393.  
  3394.             case EMPTY_TUPLE:
  3395.                 if (load_empty_tuple(self) < 0)
  3396.                     break;
  3397.                 continue;
  3398.  
  3399.             case TUPLE:
  3400.                 if (load_tuple(self) < 0)
  3401.                     break;
  3402.                 continue;
  3403.  
  3404.             case EMPTY_LIST:
  3405.                 if (load_empty_list(self) < 0)
  3406.                     break;
  3407.                 continue;
  3408.  
  3409.             case LIST:
  3410.                 if (load_list(self) < 0)
  3411.                     break;
  3412.                 continue;
  3413.  
  3414.             case EMPTY_DICT:
  3415.                 if (load_empty_dict(self) < 0)
  3416.                     break;
  3417.                 continue;
  3418.  
  3419.             case DICT:
  3420.                 if (load_dict(self) < 0)
  3421.                     break;
  3422.                 continue;
  3423.  
  3424.             case OBJ:
  3425.                 if (load_obj(self) < 0)
  3426.                     break;
  3427.                 continue;
  3428.  
  3429.             case INST:
  3430.                 if (load_inst(self) < 0)
  3431.                     break;
  3432.                 continue;
  3433.  
  3434.             case GLOBAL:
  3435.                 if (load_global(self) < 0)
  3436.                     break;
  3437.                 continue;
  3438.  
  3439.             case APPEND:
  3440.                 if (load_append(self) < 0)
  3441.                     break;
  3442.                 continue;
  3443.  
  3444.             case APPENDS:
  3445.                 if (load_appends(self) < 0)
  3446.                     break;
  3447.                 continue;
  3448.    
  3449.             case BUILD:
  3450.                 if (load_build(self) < 0)
  3451.                     break;
  3452.                 continue;
  3453.   
  3454.             case DUP:
  3455.                 if (load_dup(self) < 0)
  3456.                     break;
  3457.                 continue;
  3458.  
  3459.             case BINGET:
  3460.                 if (load_binget(self) < 0)
  3461.                     break;
  3462.                 continue;
  3463.  
  3464.             case LONG_BINGET:
  3465.                 if (load_long_binget(self) < 0)
  3466.                     break;
  3467.                 continue;
  3468.          
  3469.             case GET:
  3470.                 if (load_get(self) < 0)
  3471.                     break;
  3472.                 continue;
  3473.  
  3474.             case MARK:
  3475.                 if (load_mark(self) < 0)
  3476.                     break;
  3477.                 continue;
  3478.  
  3479.             case BINPUT:
  3480.                 if (load_binput(self) < 0)
  3481.                     break;
  3482.                 continue;
  3483.  
  3484.             case LONG_BINPUT:
  3485.                 if (load_long_binput(self) < 0)
  3486.                     break;
  3487.                 continue;
  3488.          
  3489.             case PUT:
  3490.                 if (load_put(self) < 0)
  3491.                     break;
  3492.                 continue;
  3493.  
  3494.             case POP:
  3495.                 if (load_pop(self) < 0)
  3496.                     break;
  3497.                 continue;
  3498.  
  3499.             case POP_MARK:
  3500.                 if (load_pop_mark(self) < 0)
  3501.                     break;
  3502.                 continue;
  3503.  
  3504.             case SETITEM:
  3505.                 if (load_setitem(self) < 0)
  3506.                     break;
  3507.                 continue;
  3508.  
  3509.             case SETITEMS:
  3510.                 if (load_setitems(self) < 0)
  3511.                     break;
  3512.                 continue;
  3513.  
  3514.             case STOP:
  3515.                 break;
  3516.  
  3517.             case PERSID:
  3518.                 if (load_persid(self) < 0)
  3519.                     break;
  3520.                 continue;
  3521.  
  3522.             case BINPERSID:
  3523.                 if (load_binpersid(self) < 0)
  3524.                     break;
  3525.                 continue;
  3526.  
  3527.             case REDUCE:
  3528.                 if (load_reduce(self) < 0)
  3529.                     break;
  3530.                 continue;
  3531.  
  3532.             default: 
  3533.                 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", 
  3534.                     "c", s[0]);
  3535.                 goto err;
  3536.         }
  3537.  
  3538.         break;
  3539.     }
  3540.  
  3541.     if ((err = PyErr_Occurred()) == PyExc_EOFError) {
  3542.         PyErr_SetNone(PyExc_EOFError);
  3543.         goto err;
  3544.     }    
  3545.  
  3546.     if (err) goto err;
  3547.  
  3548.     if ((len = PyList_Size(stack)) < 0) goto err;
  3549.  
  3550.     UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
  3551.     Py_INCREF(val);
  3552.  
  3553.     Py_DECREF(stack);
  3554.  
  3555.     self->stack=NULL;
  3556.     return val;
  3557.  
  3558. err:
  3559.     self->stack=NULL;
  3560.     Py_XDECREF(stack);
  3561.  
  3562.     return NULL;
  3563. }
  3564.     
  3565.  
  3566. /* No-load functions to support noload, which is used to
  3567.    find persistent references. */
  3568.  
  3569. static int
  3570. noload_obj(Unpicklerobject *self) {
  3571.     int i, len;
  3572.  
  3573.     if ((i = marker(self)) < 0) return -1;
  3574.     if ((len = PyList_Size(self->stack)) < 0) return -1;
  3575.     return DEL_LIST_SLICE(self->stack, i+1, len);
  3576. }
  3577.  
  3578.  
  3579. static int
  3580. noload_inst(Unpicklerobject *self) {
  3581.     int i, j;
  3582.     char *s;
  3583.  
  3584.     if ((i = marker(self)) < 0) return -1;
  3585.     if ((j = PyList_Size(self->stack)) < 0) return -1;
  3586.     if (DEL_LIST_SLICE(self->stack, i, j) < 0) return -1;
  3587.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3588.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3589.     return PyList_Append(self->stack, Py_None);
  3590. }
  3591.  
  3592. static int
  3593. noload_global(Unpicklerobject *self) {
  3594.     char *s;
  3595.  
  3596.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3597.     if ((*self->readline_func)(self, &s) < 0) return -1;
  3598.     return PyList_Append(self->stack, Py_None);
  3599. }
  3600.  
  3601. static int
  3602. noload_reduce(Unpicklerobject *self) {
  3603.     int len;
  3604.  
  3605.     if ((len = PyList_Size(self->stack)) < 0) return -1;
  3606.     if (DEL_LIST_SLICE(self->stack, len - 2, len) < 0) return -1;
  3607.     return PyList_Append(self->stack, Py_None);
  3608. }
  3609.  
  3610. static int
  3611. noload_build(Unpicklerobject *self) {
  3612.   int len;
  3613.  
  3614.   if ((len = PyList_Size(self->stack)) < 0) return -1;
  3615.   return DEL_LIST_SLICE(self->stack, len - 1, len);
  3616. }
  3617.  
  3618.  
  3619. static PyObject *
  3620. noload(Unpicklerobject *self) {
  3621.     PyObject *stack = 0, *err = 0, *val = 0;
  3622.     int len;
  3623.     char *s;
  3624.  
  3625.     UNLESS(stack = PyList_New(0))
  3626.         goto err;
  3627.  
  3628.     self->stack = stack;
  3629.     self->num_marks = 0;
  3630.  
  3631.     while (1) {
  3632.         if ((*self->read_func)(self, &s, 1) < 0)
  3633.             break;
  3634.  
  3635.         switch (s[0]) {
  3636.             case NONE:
  3637.                 if (load_none(self) < 0)
  3638.                     break;
  3639.                 continue;
  3640.  
  3641.             case BININT:
  3642.                  if (load_binint(self) < 0)
  3643.                      break;
  3644.                  continue;
  3645.  
  3646.             case BININT1:
  3647.                 if (load_binint1(self) < 0)
  3648.                     break;
  3649.                 continue;
  3650.  
  3651.             case BININT2:
  3652.                 if (load_binint2(self) < 0)
  3653.                     break;
  3654.                 continue;
  3655.  
  3656.             case INT:
  3657.                 if (load_int(self) < 0)
  3658.                     break;
  3659.                 continue;
  3660.  
  3661.             case LONG:
  3662.                 if (load_long(self) < 0)
  3663.                     break;
  3664.                 continue;
  3665.  
  3666.             case FLOAT:
  3667.                 if (load_float(self) < 0)
  3668.                     break;
  3669.                 continue;
  3670.  
  3671.             case BINFLOAT:
  3672.                 if (load_binfloat(self) < 0)
  3673.                     break;
  3674.                 continue;
  3675.  
  3676.             case BINSTRING:
  3677.                 if (load_binstring(self) < 0)
  3678.                     break;
  3679.                 continue;
  3680.  
  3681.             case SHORT_BINSTRING:
  3682.                 if (load_short_binstring(self) < 0)
  3683.                     break;
  3684.                 continue;
  3685.  
  3686.             case STRING:
  3687.                 if (load_string(self) < 0)
  3688.                     break;
  3689.                 continue;
  3690.  
  3691.             case EMPTY_TUPLE:
  3692.                 if (load_empty_tuple(self) < 0)
  3693.                     break;
  3694.                 continue;
  3695.  
  3696.             case TUPLE:
  3697.                 if (load_tuple(self) < 0)
  3698.                     break;
  3699.                 continue;
  3700.  
  3701.             case EMPTY_LIST:
  3702.                 if (load_empty_list(self) < 0)
  3703.                     break;
  3704.                 continue;
  3705.  
  3706.             case LIST:
  3707.                 if (load_list(self) < 0)
  3708.                     break;
  3709.                 continue;
  3710.  
  3711.             case EMPTY_DICT:
  3712.                 if (load_empty_dict(self) < 0)
  3713.                     break;
  3714.                 continue;
  3715.  
  3716.             case DICT:
  3717.                 if (load_dict(self) < 0)
  3718.                     break;
  3719.                 continue;
  3720.  
  3721.             case OBJ:
  3722.                 if (noload_obj(self) < 0)
  3723.                     break;
  3724.                 continue;
  3725.  
  3726.             case INST:
  3727.                 if (noload_inst(self) < 0)
  3728.                     break;
  3729.                 continue;
  3730.  
  3731.             case GLOBAL:
  3732.                 if (noload_global(self) < 0)
  3733.                     break;
  3734.                 continue;
  3735.  
  3736.             case APPEND:
  3737.                 if (load_append(self) < 0)
  3738.                     break;
  3739.                 continue;
  3740.  
  3741.             case APPENDS:
  3742.                 if (load_appends(self) < 0)
  3743.                     break;
  3744.                 continue;
  3745.    
  3746.             case BUILD:
  3747.                 if (noload_build(self) < 0)
  3748.                     break;
  3749.                 continue;
  3750.   
  3751.             case DUP:
  3752.                 if (load_dup(self) < 0)
  3753.                     break;
  3754.                 continue;
  3755.  
  3756.             case BINGET:
  3757.                 if (load_binget(self) < 0)
  3758.                     break;
  3759.                 continue;
  3760.  
  3761.             case LONG_BINGET:
  3762.                 if (load_long_binget(self) < 0)
  3763.                     break;
  3764.                 continue;
  3765.          
  3766.             case GET:
  3767.                 if (load_get(self) < 0)
  3768.                     break;
  3769.                 continue;
  3770.  
  3771.             case MARK:
  3772.                 if (load_mark(self) < 0)
  3773.                     break;
  3774.                 continue;
  3775.  
  3776.             case BINPUT:
  3777.                 if (load_binput(self) < 0)
  3778.                     break;
  3779.                 continue;
  3780.  
  3781.             case LONG_BINPUT:
  3782.                 if (load_long_binput(self) < 0)
  3783.                     break;
  3784.                 continue;
  3785.          
  3786.             case PUT:
  3787.                 if (load_put(self) < 0)
  3788.                     break;
  3789.                 continue;
  3790.  
  3791.             case POP:
  3792.                 if (load_pop(self) < 0)
  3793.                     break;
  3794.                 continue;
  3795.  
  3796.             case POP_MARK:
  3797.                 if (load_pop_mark(self) < 0)
  3798.                     break;
  3799.                 continue;
  3800.  
  3801.             case SETITEM:
  3802.                 if (load_setitem(self) < 0)
  3803.                     break;
  3804.                 continue;
  3805.  
  3806.             case SETITEMS:
  3807.                 if (load_setitems(self) < 0)
  3808.                     break;
  3809.                 continue;
  3810.  
  3811.             case STOP:
  3812.                 break;
  3813.  
  3814.             case PERSID:
  3815.                 if (load_persid(self) < 0)
  3816.                     break;
  3817.                 continue;
  3818.  
  3819.             case BINPERSID:
  3820.                 if (load_binpersid(self) < 0)
  3821.                     break;
  3822.                 continue;
  3823.  
  3824.             case REDUCE:
  3825.                 if (noload_reduce(self) < 0)
  3826.                     break;
  3827.                 continue;
  3828.  
  3829.             default: 
  3830.                 cPickle_ErrFormat(UnpicklingError, "invalid load key, '%s'.", 
  3831.                     "c", s[0]);
  3832.                 goto err;
  3833.         }
  3834.  
  3835.         break;
  3836.     }
  3837.  
  3838.     if ((err = PyErr_Occurred()) == PyExc_EOFError) {
  3839.         PyErr_SetNone(PyExc_EOFError);
  3840.         goto err;
  3841.     }    
  3842.  
  3843.     if (err) goto err;
  3844.  
  3845.     if ((len = PyList_Size(stack)) < 0) goto err;
  3846.  
  3847.     UNLESS(val = PyList_GetItem(stack, len - 1)) goto err;
  3848.     Py_INCREF(val);
  3849.  
  3850.     Py_DECREF(stack);
  3851.  
  3852.     self->stack=NULL;
  3853.     return val;
  3854.  
  3855. err:
  3856.     self->stack=NULL;
  3857.     Py_XDECREF(stack);
  3858.  
  3859.     return NULL;
  3860. }
  3861.     
  3862.  
  3863. static PyObject *
  3864. Unpickler_load(Unpicklerobject *self, PyObject *args) {
  3865.     UNLESS(PyArg_ParseTuple(args, "")) 
  3866.         return NULL;
  3867.  
  3868.     return load(self);
  3869. }
  3870.  
  3871. static PyObject *
  3872. Unpickler_noload(Unpicklerobject *self, PyObject *args) {
  3873.     UNLESS(PyArg_ParseTuple(args, "")) 
  3874.         return NULL;
  3875.  
  3876.     return noload(self);
  3877. }
  3878.  
  3879.  
  3880. static struct PyMethodDef Unpickler_methods[] = {
  3881.   {"load",         (PyCFunction)Unpickler_load,   1,
  3882.    "load() -- Load a pickle"
  3883.   },
  3884.   {"noload",         (PyCFunction)Unpickler_noload,   1,
  3885.    "noload() -- not load a pickle, but go through most of the motions\n"
  3886.    "\n"
  3887.    "This function can be used to read past a pickle without instantiating\n"
  3888.    "any objects or importing any modules.  It can also be used to find all\n"
  3889.    "persistent references without instantiating any objects or importing\n"
  3890.    "any modules.\n"
  3891.   },
  3892.   {NULL,              NULL}           /* sentinel */
  3893. };
  3894.  
  3895.  
  3896. static Unpicklerobject *
  3897. newUnpicklerobject(PyObject *f) {
  3898.     Unpicklerobject *self;
  3899.  
  3900.     UNLESS(self = PyObject_NEW(Unpicklerobject, &Unpicklertype))
  3901.         return NULL;
  3902.  
  3903.     self->file = NULL;
  3904.     self->arg = NULL;
  3905.     self->stack = NULL;
  3906.     self->pers_func = NULL;
  3907.     self->last_string = NULL;
  3908.     self->marks = NULL;
  3909.     self->num_marks = 0;
  3910.     self->marks_size = 0;
  3911.     self->buf_size = 0;
  3912.     self->read = NULL;
  3913.     self->readline = NULL;
  3914.     self->class_map = NULL;
  3915.  
  3916.     UNLESS(self->memo = PyDict_New()) {
  3917.        Py_XDECREF((PyObject *)self);
  3918.        return NULL;
  3919.     }
  3920.  
  3921.     Py_INCREF(f);
  3922.     self->file = f;
  3923.  
  3924.     /* Set read, readline based on type of f */
  3925.     if (PyFile_Check(f)) {
  3926.         self->fp = PyFile_AsFile(f);
  3927.         self->read_func = read_file;
  3928.         self->readline_func = readline_file;
  3929.     }
  3930.     else if (PycStringIO_InputCheck(f)) {
  3931.         self->fp = NULL;
  3932.         self->read_func = read_cStringIO;
  3933.         self->readline_func = readline_cStringIO;
  3934.     }
  3935.     else {
  3936.  
  3937.         self->fp = NULL;
  3938.         self->read_func = read_other;
  3939.         self->readline_func = readline_other;
  3940.  
  3941.         UNLESS((self->readline = PyObject_GetAttr(f, readline_str)) &&
  3942.             (self->read = PyObject_GetAttr(f, read_str))) {
  3943.             PyErr_Clear();
  3944.             PyErr_SetString( PyExc_TypeError, "argument must have 'read' and "
  3945.                 "'readline' attributes" );
  3946.         goto err;
  3947.         }
  3948.     }
  3949.  
  3950.     if(PyEval_GetRestricted()) {
  3951.     /* Restricted execution, get private tables */
  3952.     PyObject *m;
  3953.  
  3954.     UNLESS(self->class_map=PyDict_New()) goto err;
  3955.     UNLESS(m=PyImport_Import(copy_reg_str)) goto err;
  3956.     self->safe_constructors=PyObject_GetAttr(m, safe_constructors_str);
  3957.     Py_DECREF(m);
  3958.     UNLESS(self->safe_constructors) goto err;
  3959.     }
  3960.     else {
  3961.     self->class_map=class_map;
  3962.     Py_INCREF(class_map);
  3963.     self->safe_constructors=safe_constructors;
  3964.     Py_INCREF(safe_constructors);
  3965.     }
  3966.  
  3967.     return self;
  3968.  
  3969. err:
  3970.     Py_DECREF((PyObject *)self);
  3971.     return NULL;
  3972. }
  3973.  
  3974.  
  3975. static PyObject *
  3976. get_Unpickler(PyObject *self, PyObject *args) {
  3977.     PyObject *file;
  3978.   
  3979.     UNLESS(PyArg_ParseTuple(args, "O", &file))
  3980.         return NULL;
  3981.     return (PyObject *)newUnpicklerobject(file);
  3982. }
  3983.  
  3984.  
  3985. static void
  3986. Unpickler_dealloc(Unpicklerobject *self) {
  3987.     Py_XDECREF(self->readline);
  3988.     Py_XDECREF(self->read);
  3989.     Py_XDECREF(self->file);
  3990.     Py_XDECREF(self->memo);
  3991.     Py_XDECREF(self->stack);
  3992.     Py_XDECREF(self->pers_func);
  3993.     Py_XDECREF(self->arg);
  3994.     Py_XDECREF(self->last_string);
  3995.     Py_XDECREF(self->class_map);
  3996.     Py_XDECREF(self->safe_constructors);
  3997.  
  3998.     if (self->marks) {
  3999.         free(self->marks);
  4000.     }
  4001.  
  4002.     if (self->buf_size) {
  4003.         free(self->buf);
  4004.     }
  4005.     
  4006.     PyMem_DEL(self);
  4007. }
  4008.  
  4009.  
  4010. static PyObject *
  4011. Unpickler_getattr(Unpicklerobject *self, char *name) {
  4012.     if (!strcmp(name, "persistent_load")) {
  4013.         if (!self->pers_func) {
  4014.             PyErr_SetString(PyExc_AttributeError, name);
  4015.             return NULL;
  4016.         }
  4017.  
  4018.         Py_INCREF(self->pers_func);
  4019.         return self->pers_func;
  4020.     }
  4021.  
  4022.     if (!strcmp(name, "memo")) {
  4023.         if (!self->memo) {
  4024.             PyErr_SetString(PyExc_AttributeError, name);
  4025.             return NULL;
  4026.         }
  4027.  
  4028.         Py_INCREF(self->memo);
  4029.         return self->memo;
  4030.     }
  4031.  
  4032.     if (!strcmp(name, "stack")) {
  4033.         if (!self->stack) {
  4034.             PyErr_SetString(PyExc_AttributeError, name);
  4035.             return NULL;
  4036.         }
  4037.  
  4038.         Py_INCREF(self->stack);
  4039.         return self->stack;
  4040.     }
  4041.  
  4042.     if (!strcmp(name, "UnpicklingError")) {
  4043.         Py_INCREF(UnpicklingError);
  4044.         return UnpicklingError;
  4045.     }
  4046.  
  4047.     return Py_FindMethod(Unpickler_methods, (PyObject *)self, name);
  4048. }
  4049.  
  4050.  
  4051. static int
  4052. Unpickler_setattr(Unpicklerobject *self, char *name, PyObject *value) {
  4053.     if (!strcmp(name, "persistent_load")) {
  4054.         Py_XDECREF(self->pers_func);
  4055.         self->pers_func = value;
  4056.         Py_INCREF(value);
  4057.         return 0;
  4058.     }
  4059.  
  4060.     PyErr_SetString(PyExc_AttributeError, name);
  4061.     return -1;
  4062. }
  4063.  
  4064.  
  4065. static PyObject *
  4066. cpm_dump(PyObject *self, PyObject *args) {
  4067.     PyObject *ob, *file, *res = NULL;
  4068.     Picklerobject *pickler = 0;
  4069.     int bin = 0;
  4070.  
  4071.     UNLESS(PyArg_ParseTuple(args, "OO|i", &ob, &file, &bin))
  4072.         goto finally;
  4073.  
  4074.     UNLESS(pickler = newPicklerobject(file, bin))
  4075.         goto finally;
  4076.  
  4077.     if (dump(pickler, ob) < 0)
  4078.         goto finally;
  4079.  
  4080.     Py_INCREF(Py_None);
  4081.     res = Py_None;
  4082.  
  4083. finally:
  4084.     Py_XDECREF(pickler);
  4085.  
  4086.     return res;
  4087. }
  4088.  
  4089.  
  4090. static PyObject *
  4091. cpm_dumps(PyObject *self, PyObject *args) {
  4092.     PyObject *ob, *file = 0, *res = NULL;
  4093.     Picklerobject *pickler = 0;
  4094.     int bin = 0;
  4095.  
  4096.     UNLESS(PyArg_ParseTuple(args, "O|i", &ob, &bin))
  4097.         goto finally;
  4098.  
  4099.     UNLESS(file = PycStringIO->NewOutput(128))
  4100.         goto finally;
  4101.  
  4102.     UNLESS(pickler = newPicklerobject(file, bin))
  4103.         goto finally;
  4104.  
  4105.     if (dump(pickler, ob) < 0)
  4106.         goto finally;
  4107.  
  4108.     res = PycStringIO->cgetvalue(file);
  4109.  
  4110. finally:
  4111.     Py_XDECREF(pickler);
  4112.     Py_XDECREF(file);
  4113.  
  4114.     return res;
  4115. }  
  4116.   
  4117.  
  4118. static PyObject *
  4119. cpm_load(PyObject *self, PyObject *args) {
  4120.     Unpicklerobject *unpickler = 0;
  4121.     PyObject *ob, *res = NULL;
  4122.  
  4123.     UNLESS(PyArg_ParseTuple(args, "O", &ob))
  4124.         goto finally;
  4125.  
  4126.     UNLESS(unpickler = newUnpicklerobject(ob))
  4127.         goto finally;
  4128.  
  4129.     res = load(unpickler);
  4130.  
  4131. finally:
  4132.     Py_XDECREF(unpickler);
  4133.  
  4134.     return res;
  4135. }
  4136.  
  4137.  
  4138. static PyObject *
  4139. cpm_loads(PyObject *self, PyObject *args) {
  4140.     PyObject *ob, *file = 0, *res = NULL;
  4141.     Unpicklerobject *unpickler = 0;
  4142.  
  4143.     UNLESS(PyArg_ParseTuple(args, "O", &ob))
  4144.         goto finally;
  4145.  
  4146.     UNLESS(file = PycStringIO->NewInput(ob))
  4147.         goto finally;
  4148.   
  4149.     UNLESS(unpickler = newUnpicklerobject(file))
  4150.         goto finally;
  4151.  
  4152.     res = load(unpickler);
  4153.  
  4154. finally:
  4155.     Py_XDECREF(file);
  4156.     Py_XDECREF(unpickler);
  4157.  
  4158.     return res;
  4159. }
  4160.  
  4161.  
  4162. static char Unpicklertype__doc__[] = 
  4163. "Objects that know how to unpickle";
  4164.  
  4165. static PyTypeObject Unpicklertype = {
  4166.     PyObject_HEAD_INIT(NULL)
  4167.     0,                            /*ob_size*/
  4168.     "Unpickler",                  /*tp_name*/
  4169.     sizeof(Unpicklerobject),              /*tp_basicsize*/
  4170.     0,                            /*tp_itemsize*/
  4171.     /* methods */
  4172.     (destructor)Unpickler_dealloc,        /*tp_dealloc*/
  4173.     (printfunc)0,         /*tp_print*/
  4174.     (getattrfunc)Unpickler_getattr,       /*tp_getattr*/
  4175.     (setattrfunc)Unpickler_setattr,       /*tp_setattr*/
  4176.     (cmpfunc)0,           /*tp_compare*/
  4177.     (reprfunc)0,          /*tp_repr*/
  4178.     0,                    /*tp_as_number*/
  4179.     0,            /*tp_as_sequence*/
  4180.     0,            /*tp_as_mapping*/
  4181.     (hashfunc)0,          /*tp_hash*/
  4182.     (ternaryfunc)0,               /*tp_call*/
  4183.     (reprfunc)0,          /*tp_str*/
  4184.  
  4185.     /* Space for future expansion */
  4186.     0L,0L,0L,0L,
  4187.     Unpicklertype__doc__ /* Documentation string */
  4188. };
  4189.  
  4190. static struct PyMethodDef cPickle_methods[] = {
  4191.   {"dump",         (PyCFunction)cpm_dump,         1,
  4192.    "dump(object, file, [binary]) --"
  4193.    "Write an object in pickle format to the given file\n"
  4194.    "\n"
  4195.    "If the optional argument, binary, is provided and is true, then the\n"
  4196.    "pickle will be written in binary format, which is more space and\n"
  4197.    "computationally efficient. \n"
  4198.   },
  4199.   {"dumps",        (PyCFunction)cpm_dumps,        1,
  4200.    "dumps(object, [binary]) --"
  4201.    "Return a string containing an object in pickle format\n"
  4202.    "\n"
  4203.    "If the optional argument, binary, is provided and is true, then the\n"
  4204.    "pickle will be written in binary format, which is more space and\n"
  4205.    "computationally efficient. \n"
  4206.   },
  4207.   {"load",         (PyCFunction)cpm_load,         1,
  4208.    "load(file) -- Load a pickle from the given file"},
  4209.   {"loads",        (PyCFunction)cpm_loads,        1,
  4210.    "loads(string) -- Load a pickle from the given string"},
  4211.   {"Pickler",      (PyCFunction)get_Pickler,      1,
  4212.    "Pickler(file, [binary]) -- Create a pickler\n"
  4213.    "\n"
  4214.    "If the optional argument, binary, is provided and is true, then\n"
  4215.    "pickles will be written in binary format, which is more space and\n"
  4216.    "computationally efficient. \n"
  4217.   },
  4218.   {"Unpickler",    (PyCFunction)get_Unpickler,    1,
  4219.    "Unpickler(file) -- Create an unpickler"},
  4220.   { NULL, NULL }
  4221. };
  4222.  
  4223.  
  4224. #define CHECK_FOR_ERRORS(MESS) \
  4225. if(PyErr_Occurred()) { \
  4226.     PyObject *__sys_exc_type, *__sys_exc_value, *__sys_exc_traceback; \
  4227.     PyErr_Fetch( &__sys_exc_type, &__sys_exc_value, &__sys_exc_traceback); \
  4228.     fprintf(stderr, # MESS ":\n\t"); \
  4229.     PyObject_Print(__sys_exc_type, stderr,0); \
  4230.     fprintf(stderr,", "); \
  4231.     PyObject_Print(__sys_exc_value, stderr,0); \
  4232.     fprintf(stderr,"\n"); \
  4233.     fflush(stderr); \
  4234.     Py_FatalError(# MESS); \
  4235. }
  4236.  
  4237.  
  4238. static int
  4239. init_stuff(PyObject *module, PyObject *module_dict) {
  4240.     PyObject *string, *copy_reg;
  4241.  
  4242. #define INIT_STR(S) UNLESS(S ## _str=PyString_FromString(#S)) return -1;
  4243.  
  4244.     INIT_STR(__class__);
  4245.     INIT_STR(__getinitargs__);
  4246.     INIT_STR(__dict__);
  4247.     INIT_STR(__getstate__);
  4248.     INIT_STR(__setstate__);
  4249.     INIT_STR(__name__);
  4250.     INIT_STR(__main__);
  4251.     INIT_STR(__reduce__);
  4252.     INIT_STR(write);
  4253.     INIT_STR(__safe_for_unpickling__);
  4254.     INIT_STR(append);
  4255.     INIT_STR(read);
  4256.     INIT_STR(readline);
  4257.     INIT_STR(copy_reg);
  4258.     INIT_STR(dispatch_table);
  4259.     INIT_STR(safe_constructors);
  4260.     INIT_STR(__basicnew__);
  4261.  
  4262.     UNLESS(copy_reg = PyImport_ImportModule("copy_reg"))
  4263.         return -1;
  4264.  
  4265.     /* These next few are special because we want to use different
  4266.        ones in restricted mode. */
  4267.  
  4268.     UNLESS(dispatch_table = PyObject_GetAttr(copy_reg, dispatch_table_str))
  4269.         return -1;
  4270.  
  4271.     UNLESS(safe_constructors = PyObject_GetAttr(copy_reg,
  4272.                         safe_constructors_str))
  4273.         return -1;
  4274.  
  4275.     Py_DECREF(copy_reg);
  4276.  
  4277.     UNLESS(class_map = PyDict_New()) return -1;
  4278.  
  4279.     /* Down to here ********************************** */
  4280.  
  4281.     UNLESS(string = PyImport_ImportModule("string"))
  4282.         return -1;
  4283.  
  4284.     UNLESS(atol_func = PyObject_GetAttrString(string, "atol"))
  4285.         return -1;
  4286.  
  4287.     Py_DECREF(string);
  4288.  
  4289.     UNLESS(empty_tuple = PyTuple_New(0))
  4290.         return -1;
  4291.  
  4292.     UNLESS(PicklingError = PyString_FromString("cPickle.PicklingError"))
  4293.         return -1;
  4294.  
  4295.     if (PyDict_SetItemString(module_dict, "PicklingError", 
  4296.         PicklingError) < 0)
  4297.         return -1;
  4298.  
  4299.     UNLESS(UnpicklingError = PyString_FromString("cPickle.UnpicklingError"))
  4300.         return -1;
  4301.  
  4302.     if (PyDict_SetItemString(module_dict, "UnpicklingError",
  4303.         UnpicklingError) < 0)
  4304.         return -1;
  4305.  
  4306.     PycString_IMPORT;
  4307.  
  4308.     return 0;
  4309. }
  4310.  
  4311. void
  4312. initcPickle Py_PROTO((void)) {
  4313.     PyObject *m, *d, *v;
  4314.     char *rev="1.48";
  4315.     PyObject *format_version;
  4316.     PyObject *compatible_formats;
  4317.  
  4318.     Picklertype.ob_type = &PyType_Type;
  4319.     Unpicklertype.ob_type = &PyType_Type;
  4320.  
  4321.     /* Create the module and add the functions */
  4322.     m = Py_InitModule4("cPickle", cPickle_methods,
  4323.                      cPickle_module_documentation,
  4324.                      (PyObject*)NULL,PYTHON_API_VERSION);
  4325.  
  4326.     /* Add some symbolic constants to the module */
  4327.     d = PyModule_GetDict(m);
  4328.     PyDict_SetItemString(d,"__version__", v = PyString_FromString(rev));
  4329.     Py_XDECREF(v);
  4330.  
  4331. #ifdef FORMAT_1_3
  4332.     format_version = PyString_FromString("1.3");
  4333.     compatible_formats = Py_BuildValue("[sss]", "1.0", "1.1", "1.2");
  4334. #else
  4335.     format_version = PyString_FromString("1.2");
  4336.     compatible_formats = Py_BuildValue("[ss]", "1.0", "1.1");
  4337. #endif
  4338.  
  4339.     PyDict_SetItemString(d, "format_version", format_version);
  4340.     PyDict_SetItemString(d, "compatible_formats", compatible_formats);
  4341.     Py_XDECREF(format_version);
  4342.     Py_XDECREF(compatible_formats);
  4343.  
  4344.     init_stuff(m, d);
  4345.     CHECK_FOR_ERRORS("can't initialize module cPickle");
  4346. }
  4347.